我想知道 POST 方法而不是 GET 方法。发送值怎么写。我在 GET 示例中找不到发送要放置的值的位置
朋友可以示范给我看看吗?
我写 POST 方法
POST 方法示例:
private POST()
{
HttpPost httpRequest2 = new HttpPost("ip");
List<NameValuePair> params2 = new ArrayList<NameValuePair>();
params2.add(new BasicNameValuePair("Q1","b"));
//Ready to send the value of the
try
{
httpRequest2.setEntity(new UrlEncodedFormEntity(params2, HTTP.UTF_8));
httpResponse2 = new DefaultHttpClient()
.execute(httpRequest2);
if (httpResponse2.getStatusLine().getStatusCode() == 200)
{
String strResult2 = EntityUtils.toString(httpResponse2.getEntity());
return strResult2;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
网络上找到的GET教学方法。
GET 方法示例:
public StringGet() throws Exception {
String strResult = "";
String httpUrl = "ip";
HttpGet httpRequest = new HttpGet(httpUrl);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
strResult = EntityUtils.toString(httpResponse.getEntity());
tv.setText(strResult);
} else {
tv.setText("fail");
}
return strResult;
}
}