【问题标题】:Couldnt get response from json无法从 json 获得响应
【发布时间】:2013-11-25 11:54:46
【问题描述】:

我正在尝试提升using this

这是我的代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/elevation/json");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("locations", "40.714728,-73.998672"));
    nameValuePairs.add(new BasicNameValuePair("sensor", "true"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    System.out.println("HTTP RESPONSE  " + response.toString());
    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");
    System.out.println("RESPONSE " + responseString);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    System.out.println("ClientProtocolException " + e.toString());
} catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("IOException " + e.toString());
}

我收到以下错误

无效请求缺少路径或位置参数

【问题讨论】:

    标签: android json http xmlhttprequest httpresponse


    【解决方案1】:
    HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/elevation/json?locations=39.0000,100.0000&sensor=false");
    

    并从代码中删除名称值对。

    【讨论】:

      【解决方案2】:

      您正在使用数据执行 HTTP POST 请求。 API 似乎指定了一个 HTTP GET 请求(带有 URL 中的数据)。

      一个 GET 请求更容易制定 - 你可以使用字符串连接来构造你需要的 URL:

      http://maps.googleapis.com/maps/api/elevation/json?locations=<lat,lon>&sensor=<bool>
      

      【讨论】:

      • 你知道怎么做吗?这是 HTTP 的新手
      【解决方案3】:

      您可以使用简单的 HTTP GET 请求获得响应。

      http://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&sensor=true
      

      【讨论】:

        【解决方案4】:
        String url = "http://maps.googleapis.com/maps/api/elevation/json"
        String[] parameter = {"locations", "40.714728,-73.998672", "sensor", "true"};
        HttpParams httpParameters = new BasicHttpParams();
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        // Prepare a request object
        HttpGet httpget = new HttpGet(url+getUrlParameter(parameter));
        // Execute the request
        HttpResponse response;
        response = httpclient.execute(httpget);
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        
        
        
        public String getUrlParameter(String[] parameter){
            for(int i=0 ; i<parameter.length ; i=i+2){
                if(i == 0)
                    urlParameter = "?" + parameter[i] + "=" + parameter[i+1];
                else
                    urlParameter = urlParameter + "&" + parameter[i] + "=" + parameter[i+1]; 
            }
            return urlParameter;
        }
        

        这段代码来自我自己的 jsonlibrary,它应该适合你。从这里 HttpEntity 实体 将充满数据。您必须将它们分开才能在您的代码中使用。另请注意,此 http 作品必须在 try-catch 博客上,以便在不良条件下捕获异常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-05
          • 1970-01-01
          • 2020-08-25
          • 2021-07-18
          相关资源
          最近更新 更多