Volley获取Json数据异常

这两天用Volley加载网上数据碰到两个奇怪的问题,百思不得其解,现在还没弄明白为什么会出现这种问题,先记录在此:

  1. 调用Volley方法StringRequest获取网页数据,调试的时候在某次添加断点运行异常,去掉断点则运行正常
  2. 调用Volley方法JsonObjectRequest获取http://www.weather.com.cn/data/sk/101010100.html数据异常,但获取http://www.kuaidi100.com/query?type=快递公司代号&postid=快递单号http://api.map.baidu.com/telematics/v3/trafficEvent?location=%E5%8C%97%E4%BA%AC&output=json&ak=E4805d16520de693a3fe707cdc962045数据均正常

1.接下来我们就来先说说第一种情况,先上测试时候用的代码

public void TestStringRquestByGet() {
    StringRequest request = new StringRequest("https://www.baidu.com/",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("TAG", response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("TAG", error.getMessage(), error);
                }
            });
    RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
    mQueue.add(request);
}

在调试上面代码的时候,我们会进入到Volley的jar包中BasicNetwork.java单元中的performRequest方法,该方法会调用这么依据代码setConnectionParametersForRequest(connection, request);
我们进入到setConnectionParametersForRequest方法后把断点设置在
下面这句代码上connection.setRequestMethod(“GET”);
运行则报错,去掉断点正常,好晕

public HttpResponse performRequest(Request request, Map additionalHeaders) throws IOException, AuthFailureError {
    ...
    setConnectionParametersForRequest(connection, request);
    ...
}
static void setConnectionParametersForRequest(HttpURLConnection connection, Request request) throws IOException, AuthFailureError {
        switch (request.getMethod()) {
            ...
            case Method.GET:
                connection.setRequestMethod("GET");
                break;
            ...
        }
    }

这是当时调试时候截的图片,如下:
Volley获取Json数据异常_第1张图片
上图代码设置断点后,执行报错,报错如下图
Volley获取Json数据异常_第2张图片

网上查了一下,也有人有同样问题Volley获取Json数据异常_第3张图片
http://stackoverflow.com/questions/19766327/android-java-net-protocolexception-connection-already-established
真是见了鬼了啊,有谁知道原因的麻烦告知下啊

2.第二个问题,同样先上代码

public void TestJsonObjectRequest() {
    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.GET,
            "http://www.weather.com.cn/data/sk/101010100.html",
            null,
            new Response.Listener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("TAG", error.getMessage(), error);
                }
            });
    RequestQueue mQueue = Volley.newRequestQueue(this);
    mQueue.add(request);
}

很简单的一段代码,就是用于获取网上的一段天气预报数据(Json格式),我们用浏览器打开查看发现数据完全正常Volley获取Json数据异常_第4张图片,但运行的时候报错,经过调试我们发现异常是BasicNetwork.java单元中的方法entityToBytes的代码引起的,如下图Volley获取Json数据异常_第5张图片
从图上我们知道引起异常的代码就是这句while ((count = in.read(buffer)) != -1)代码导致的(volley原程序中没有try catch,这个是我自己加的),因此导致该方法entityToBytes没有返回值,从而导致取不到数据。

解决办法就是:像我一样将上面代码用try catch包裹起来即可,但现在不知道为什么访问其他网页的json数据又不会报异常呢,比如这两个接口都正常http://www.kuaidi100.com/query?type=快递公司代号&postid=快递单号http://api.map.baidu.com/telematics/v3/trafficEvent?location=%E5%8C%97%E4%BA%AC&output=json&ak=E4805d16520de693a3fe707cdc962045。真是坑爹有没有,还有为什么读取数据会报java.io.EOFException异常

你可能感兴趣的:(Android)