首页 > 解决方案 > 如何解决 volley 中的基本网络错误

问题描述

当我尝试通过 wifi 访问此功能时,工作正常,但在移动网络上我收到此错误...D/Volley: [3641] BasicNetwork.logSlowRequests: HTTP response for request=<[] https://fruitnveggiemart .com/public/api/register 0x95bac5ee 正常 14>

* 2020-07-30 16:16:54.671 24632-24863/com.fruitnveggiemart.ind E/Volley:[3640] BasicNetwork.performRequest: https ://fruitnveggiemart.com/public/api/register 的意外响应代码 409 2020-07-30 16:16:54.678 24632-24632/com.fruitnveggiemart.ind I/TouchMotionHandler:处理程序 onDetached 2020-07-30 16:16:54.703 24632-24632/com.fruitnveggiemart.ind V/字体:CurFontPath: /system/fonts/Roboto-Regular.ttf 2020-07-30 16:16:54.703 24632-24632/com.fruitnveggiemart.ind D/字体:切换到 android 源 ttf 2020-07-30 16:16:54.711 24632- 24632/com.fruitnveggiemart.ind W/System.err:com.android.volley.ClientError 2020-07-30 16:16:54.712 24632-24632/com.fruitnveggiemart.ind W/System.err:在 com.android。 volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:199) 2020-07-30 16:16:54.712 24632-24632/com.fruitnveggiemart.ind W/System.err: 在 com.android.volley.NetworkDispatcher.processRequest( NetworkDispatcher.java:131) 2020-07-30 16:16:54.712 24632-24632/com.fruitnveggiemart.ind W/System.err:在 com.android.volley。NetworkDispatcher.processRequest(NetworkDispatcher.java:111) 2020-07-30 16:16:54.712 24632-24632/com.fruitnveggiemart.ind W/System.err: 在 com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java: 90) 2020-07-30 16:17:57.841 24632-24699/com.fruitnveggiemart.ind W/DpmTcmClient:读取失败字节计数:-1 2020-07-30 16:17:57.996 24632-24699/com.fruitnveggiemart。 ind W/DpmTcmClient:读取失败的字节 countRead:-1 2020-07-30 16:18:02.601 24632-24632/com.fruitnveggiemart.ind D/DecorView:onConfigurationChanged displayWindowDecor = false,mWindowMode = 1,pkg = com.fruitnveggiemart。 ind 2020-07-30 16:18:02.602 24632-24632/com.fruitnveggiemart.ind I/DecorView: onConfigurationChanged mBackgroundPadding = Rect(32, 32 - 32, 32), mFramePadding = Rect(0, 0 - 0, 0) , pkg = com.fruitnveggiemart.ind 2020-07-30 16:18:02.626 24632-24864/com。fruitnveggiemart.ind I/DpmTcmClient: RegisterTcmMonitor from: $Proxy0 2020-07-30 16:18:02.628 24632-24632/com.fruitnveggiemart.ind I/TouchMotionHandler: handler onAttached 2020-07-30 16:18:10.229 24632-24864 /com.fruitnveggiemart.ind D/Volley:[3641] BasicNetwork.logSlowRequests:请求的 HTTP 响应=<[]https://fruitnveggiemart.com/public/api/register84226696 2020-07-30 16:18:10.362 24632-27303/com.fruitnveggiemart.ind V/FA: onActivityCreated 2020-07-30 16:18:10.393 24632-24632/com.fruitnveggiemart.ind I/AppCompatViewInflater: app:主题现已弃用。请改用 android:theme。2020-07-30 16:18:10.433 24632-27305/com.fruitnveggiemart.ind V/FA:连接尝试已在进行中 2020-07-30 16:18:10.475 24632-27305/com.fruitnveggiemart.ind V/FA : 活动恢复, 时间: 84226848 2020-07-30 16:18:10.492 24632-24632/com.fruitnveggiemart.ind I/TouchMotionHandler: handler onAttached 2020-07-30 16:18:10.549 24632-27305/com.fruitnveggiemart. ind V/FA:连接尝试已在进行中 2020-07-30 16:18:10.552 24632-27305/com.fruitnveggiemart.ind V/FA:连接尝试已在进行中 2020-07-30 16:18:10.697 24632- 27305/com.fruitnveggiemart.ind D/FA:

''' public void registrationNetworkCall() {

    try {

        pDialog.setTitle("Registering..");
        pDialog.show();
        JSONObject json = new JSONObject();

        json.put("name", et_name.getText().toString());
        json.put("email", et_email.getText().toString());
        json.put("mobile_no", numberPHone);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.BASE_URL + Constants.REGISTER_USER, json,
                response -> {

                    CommonMethod.hideDialog(pDialog);
                    try {
                        boolean success = response.getBoolean("success");
                        if (success) {
                            String message = response.optString("message");
                            String token = response.optString("successtoken");
                            int userId = response.optInt("user_id");
                            objCustomSharedPrefs.storeStringValueInPrefs(Keys.BASIC_AUTH, token);
                            objCustomSharedPrefs.storeIntValueInPrefs(Keys.MEMBER_ID, userId);
                            CommonMethod.showToast(mContext, message);

                            Intent intent = new Intent(UserProfileBackup.this, UserDashboard.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();

                        } else {
                            String error = response.optString("error");
                            CommonMethod.showToast(mContext, error);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse (VolleyError error){
                CommonMethod.hideDialog(pDialog);
                if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                    //This indicates that the reuest has either time out or there is no connection
                    Toast.makeText(mContext, "nocnnecitno error", Toast.LENGTH_SHORT).show();

                } else if (error instanceof AuthFailureError) {
                    // Error indicating that there was an Authentication Failure while performing the request
                    Toast.makeText(mContext, "AuthFailureError error", Toast.LENGTH_SHORT).show();
                } else if (error instanceof ServerError) {



                    //Indicates that the server responded with a error response
                    Toast.makeText(mContext, "ServerError error", Toast.LENGTH_SHORT).show();
                } else if (error instanceof NetworkError) {
                    //Indicates that there was network error while performing the request
                    Toast.makeText(mContext, "NetworkError error", Toast.LENGTH_SHORT).show();
                } else if (error instanceof ParseError) {
                    // Indicates that the server response could not be parsed
                    Toast.makeText(mContext, "ParseError error", Toast.LENGTH_SHORT).show();
                }

                error.printStackTrace();
            }
        });
        AppController.getInstance().addToRequestQueue(jsonObjectRequest, "OTPREQUESTING");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

'''

标签: androidjsonandroid-volley

解决方案


我想这会对你有所帮助。尝试这个

                final JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,
                        new JSONObject(strJsonMapObject), new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {


                    }
                }, new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        CustomDiallog.StopLoading();

                        CustomDiallog.showVolleyError(activity.this, Error_log.handleVolleyError(error));
                    }
                }) {
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Content-Type", "application/json; charset=utf-8");
                        headers.put("User-agent", System.getProperty("http.agent"));

                        return headers;
                    }
                };
                GlobalVariables.volleyJsonObjectRequest(postRequest);
                AppController.getInstance().addToRequestQueue(postRequest);

在添加请求对象之前调用此方法开玩笑。等待 5 秒

                public static void volleyJsonObjectRequest(JsonObjectRequest req) {
                    int socketTimeout = 5000;                                  // 5 seconds. You can change it
                    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
                            2,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
                    req.setRetryPolicy(policy);
                }

推荐阅读