httpget is not working for query string

我与影子孤独终老i 提交于 2019-12-04 19:03:21

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);

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

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