更新时间:2021-12-25 23:03:20
使用此方法发出POST请求:
Use this method to make POST Request :
public String makePOSTRequest(String url, List<NameValuePair> nameValuePairs) {
String response = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOGTAG, "POST Response >>> " + response);
return response;
}
用法:
在Java中:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json",jsonObject.toString()));
String response = makePOSTRequest(String url, nameValuePairs );
服务器端PHP:
$jsonInput = $_POST['json'];
json_decode($jsonInput);