如何在android中为HttpGet请求添加NameValuePairs

Dev*_*ath 2 android http-get

在线: httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));

我收到的错误是: The method setEntity(UrlEncodedFormEntity) is undefined for the type HttpGet


码:

        HttpClient httpclient = new DefaultHttpClient();
        // Add the header data for the request
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("phonenumber","12345"));
        nameValuePairs.add(new BasicNameValuePair("authtoken", "12345"));
        HttpGet httpget = new HttpGet(url);
        httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httpget);
Run Code Online (Sandbox Code Playgroud)

ato*_*tok 6

GET请求没有可以包含实体的主体,您要包含的参数应该内置到URL本身中.

一个干净的方法是使用URIBuilder:

URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost(host).setPort(port).setPath(yourpath)
.setParameter("parts", "all")
.setParameter("action", "finish");
Run Code Online (Sandbox Code Playgroud)