0

GETメソッドではなく、POSTメソッドについて知りたいです。書き方に価値を発信。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 に答える