0

我想知道 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;  
        }  

    }  
4

1 回答 1

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());
}
于 2012-12-08T18:24:24.417 回答