httpget不适用于查询字符串

时间:2013-01-22 10:56:08

标签: android http-get

我正在通过查询字符串获取数据。我已经完成了httppost,但不是我想使用httpget。但我无能为力

 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();
请专家帮帮我

2 个答案:

答案 0 :(得分:3)

您可以通过两种方式完成此操作:

使用URI构建器:

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

自己创建网址字符串:

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

答案 1 :(得分:1)

您可以尝试以下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;
}

并在URL中创建一个字符串构建器并将其添加到HTTPGet方法 该代码片段正在关注

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

响应是字符串类型