我想知道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;
}
}
答案 0 :(得分:1)
以下是通过HttpGet方法
发送数据的示例代码String webserviceurl = "your_webservice_URL";
InputStream is;
List<NameValuePair> lstAddToken = new ArrayList<NameValuePair>();
lstAddToken.add(new BasicNameValuePair("parameter1",value1));
lstAddToken.add(new BasicNameValuePair("parameter2",value2));
//add parameters to the URL
webserviceurl += "?";
String paramString = URLEncodedUtils.format(lstAddToken, "utf-8");
webserviceurl += paramString;
//Call the webservice using HttpGet with parameters and get the response from webservice
try
{
HttpGet loginHttpget = new HttpGet(webserviceurl);
HttpClient objHttpClient = new DefaultHttpClient();
HttpResponse response = objHttpClient.execute(loginHttpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
String result= convertStreamToString(is);
}
catch (Throwable t)
{
Log.e("log_tag", "Error converting result "+t.toString());
}