1

我正在努力通过查询字符串获取数据。我已经完成了 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();

请专家帮助我

4

2 回答 2

3

您可以通过 2 种方式做到这一点:

使用 URI 构建器:

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

自己创建 url 字符串:

String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
uri = new Uri("http://10.0.2.2:8080/r/r.php?" + paramString);
于 2013-01-22T11:27:42.940 回答
1

您可以尝试以下 HTTP 获取

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 中创建一个字符串构建器并将其添加到 Snippet 所遵循的 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());    

响应是字符串类型

于 2013-01-22T11:08:37.017 回答