Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android httpget不适用于查询字符串_Android_Http Get - Fatal编程技术网

Android httpget不适用于查询字符串

Android httpget不适用于查询字符串,android,http-get,Android,Http Get,我正在通过查询字符串获取数据。我已经完成了httppost,但还没有完成,我正在尝试使用httpget。但我不能 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("key", "android developer")); String k = "an

我正在通过查询字符串获取数据。我已经完成了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();
List nameValuePairs=new ArrayList();
添加(新的BasicNameValuePair(“key”,“android开发者”);
String k=“android开发者”;
URI=新的URI(“http://10.0.2.2:8080/r/r.php");
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(uri);
//setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httpget);
HttpEntity=response.getEntity();
is=entity.getContent();

请专家帮助我

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

响应为字符串类型

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

响应为字符串类型

您可以通过两种方式执行此操作:

使用URI生成器:

自己创建url字符串:


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

使用URI生成器:

自己创建url字符串:


我正在使用SalesForce API,需要将参数“q”设置为“从帐户中选择名称、Id、帐户\人员”。使用HttpClient版本4.2.5设置参数会导致错误页面,而使用带有3.1 API的“setQueryString”会导致JSON。。。有什么线索吗?请不要把SQL查询放在URL中。这将是一个灾难时,去生活。SQL注入!如果您有问题,发布一个问题,评论其他人的线程不方便。我正在使用SalesForce API,需要将参数“q”设置为“从帐户中选择名称、Id、帐户\人员”。使用HttpClient版本4.2.5设置参数会导致错误页面,而使用带有3.1 API的“setQueryString”会导致JSON。。。有什么线索吗?请不要把SQL查询放在URL中。这将是一个灾难时,去生活。SQL注入!如果你有问题,发表一个问题,评论别人的帖子是不方便的。
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);