Android volley NoConnection错误:java.io.IOException,java.io.EOFException

时间:2018-01-05 13:31:44

标签: android json android-volley

StringRequest stringRequest = new StringRequest(Request.Method.POST, UrlEndPoints.interAd,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.e("Response::", response);
                        try {
                            int success;

                            JSONObject jsonObject = new JSONObject(response);
                            Log.e("jObj_AppURL", String.valueOf(jsonObject));

                            success = jsonObject.getInt(SUCCESS);
                            if (success == 1) {
                                JSONArray data = jsonObject.getJSONArray(RESPONSE);
                                for (int i = 0; i < data.length(); i++) {
                                    JSONObject c = data.getJSONObject(i);
                                    appImg = c.getString(APPIMG);
                                    appName = c.getString(APPNAME);
                                    appPackages = c.getString(PKG);

                                    InterAdModel interAdModel = new InterAdModel();
                                    interAdModel.setAppImg(appImg);
                                    interAdModel.setAppName(appName);
                                    interAdModel.setAppPackages(appPackages);

                                    interAdModelArrayList.add(interAdModel);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override

                    public void onErrorResponse(VolleyError error) {
                        Log.e("erorr::", error.toString());
                        Toast.makeText(SplashActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                //for check service is correctly functioning use package name "com.whatsweb.whatscan.whatsappweb.whatscanforwhatweb"
                Map<String, String> params = new HashMap<>();
                params.put("task", "get_interad_apps");
                params.put("package", getPackageName());
                return params;
            }
        };
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        RequestQueue requestQueue = Volley.newRequestQueue(SplashActivity.this);
        requestQueue.add(stringRequest);

我在解析JSON对象时遇到错误。我正在添加两个字段,即。使用Map的应用程序的任务和包名称,但它总是抛出异常。我很少得到实际数据。有时它只会抛出EOFException而有时会出现NoConnection错误。

1 个答案:

答案 0 :(得分:0)

阅读NoConnectionError

  

错误表示无法建立连接   执行排球请求。

@SuppressWarnings("serial")
public class NoConnectionError extends NetworkError {
    public NoConnectionError() {
        super();
    }

    public NoConnectionError(Throwable reason) {
        super(reason);
    }
}

你应该添加

 public void onErrorResponse(VolleyError error) 
 {
    if (error instanceof NoConnectionError) 
    {
      // Your Code
    }
 }

您可以查看