【问题标题】:HttpGet is returning null for a specific urlHttpGet 为特定 url 返回 null
【发布时间】:2015-12-16 07:30:48
【问题描述】:

当 URL="http://rest.riob.us/v3/search/474" 时,我的以下代码可以正常工作。

但是对于 URL="http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/onibus/474" 返回 null。

两个 URL 在 webbrowser 上都可以正常工作。

这是我的代码:

public class HttpGetAsyncTask  extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {

    return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {

    Log.w("TAG", "result: " + result);
}

public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        Log.v("TAG", "inputStream: " + inputStream);
        Log.e("TAG", "httpResponse.getEntity().getContentLength(): " + httpResponse.getEntity().getContentLength());

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("TAG", e.getLocalizedMessage());
    }

    return result;
} 

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

}

可能出了什么问题?提前谢谢

【问题讨论】:

  • logcat 或 stacktrace
  • logcat for URL="rest.riob.us/v3/search/474": 09-19 10:52:17.684: V/TAG(700): inputStream: org.apache.http.conn.EofSensorInputStream@410af718 09-19 10:52:17.684: E/TAG(700): httpResponse.getEntity().getContentLength(): 3238 09-19 10:52:20.382: W/TAG(700): result: [{"line":"474","order":"C47499","speed":55,"direction":271,"latitude":-22.999784,"longitude":-43.35051,"sense":"CARIOCA (VIA ESTRADA BENVINDO DE NOVAES) X PIABAS","timeStamp":"2015-09-19T13:50:38.000Z"}]
  • logcat for URL="dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/…": 09-19 11:10:39.143: V/TAG(757): inputStream: org.apache.http.conn.EofSensorInputStream@410f4b90 09-19 11:10:39.152: E/TAG(757): httpResponse.getEntity().getContentLength(): -1 09-19 11:10:44.392: W/TAG(757): result:

标签: android json http-get androidhttpclient


【解决方案1】:

如果您正在为 API 或 JSON 执行网络请求,请查看 VOLLEY 库。它使用起来超级简单,它为您处理队列、异步和缓存。示例:

RequestQueue queue = Volley.newRequestQueue(this);

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //DO WITH RESPONSE
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //CRAP ERROR
            Log.e("Volley", error.toString());
        }
    });
queue.add(request);

说真的,如果您要请求 JSON 数据,使用 Volley 就简单多了!这是 JSON 请求所需的所有代码,它还提供其他内置的图像或字符串请求类型,甚至允许自定义类型。

【讨论】:

  • 但是为什么代码会以它的方式运行? (在哪里)记录了这种行为?如何在现有代码中解决它?应始终尝试回答问题,即使将(相关)放在一边。
  • 当有一个出色的库可以为您执行 Web 请求时,我不会花时间调试其他人编写的蹩脚代码。没有人会浪费时间编写 HttpClient 调用,除非他们必须这样做,这是我必须为自定义文件传输所做的。调试代码只会促进糟糕的开发和浪费时间。
  • 没关系。我也不会。还有很多值得回答的问题。然而,答案应该是答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多