httpget is not working for query string

谁都会走 提交于 2019-12-06 12:29:56

问题


i am working to get data by query string . i have done with httppost but not i am tring to get using httpget . but i am unable.

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("key", "android developer"));

            String k = "android developer";
             URI uri = new URI("http://10.0.2.2:8080/r/r.php");


             HttpClient httpclient = new DefaultHttpClient();
             HttpGet httpget = new HttpGet(uri);
             //httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httpget);
             HttpEntity entity = response.getEntity();
             is = entity.getContent();

please expert help me


回答1:


You can do this via 2 ways:

use a URI builder:

new Uri.Builder()
    .scheme("http")
    .authority("foo.com")
    .path("someservlet")
    .appendQueryParameter("param1", foo)
    .appendQueryParameter("param2", bar)
    .build();

create the url string yourself:

String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
uri = new Uri("http://10.0.2.2:8080/r/r.php?" + paramString);



回答2:


you can try the Following HTTP get

public String[] doHttpGetWithCode(String url, HashMap<String, String> headerParam) throws Exception {
    String[] result = new String[2];
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);     
    httpget.addHeader("language", Constants.DEVICE_LANGUAGE);       
    Iterator myIterator = headerParam.keySet().iterator();
    while(myIterator.hasNext()) {
        String key=(String)myIterator.next();
        String value=(String)headerParam.get(key);        
        httpget.addHeader(key, value);
    }       
    HttpResponse response;
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    result[0] = response.getStatusLine().getStatusCode()+"";
    result[1] = sb.toString();
    return result;
}

and in URL you creat a string builder and add it to HTTPGet Method that Snippet is Following

url = new StringBuilder();          
    url.append(Constants.WEB_SERVICE_URL_PREFIX)
        .append("resources/register?")
        .append("device_id=")           .append(Constants.DEVICE_ID)
        .append("&device_serial=")      .append(Constants.DEVICE_SERIAL)
        .append("&nickname=")           .append(nickname.getText().toString())
        .append("&email=")              .append(email.getText().toString())
        .append("&password=")           .append(pass.getText().toString());
response = Network.getInstance().dohttpPostWithCode(url.toString());    

response is of String type



来源:https://stackoverflow.com/questions/14456958/httpget-is-not-working-for-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!