【问题标题】:how to fix Volley timeout Error in android如何修复Android中的Volley超时错误
【发布时间】:2019-08-08 18:48:05
【问题描述】:

当我尝试运行我的代码时,它给了我有问题的错误 首先,它给了我一个不允许明文 HTTP 的错误然后我尝试通过将此行添加到清单文件来修复它 android:usesCleartextTraffic="true" 在此之后,它给我一个超时错误 我也尝试这些来修复它 click here 但它不工作 这是我的代码

private void Login() {
        ID=loginID.getText().toString();
        password=loginPassword.getText().toString();

        if (ID.isEmpty()){
            loginID.setError("This field cannot be empty");
            loginPassword.setText("");
        }else if (password.isEmpty()){
            loginPassword.setError("This field cannot be empty");
        }else {
            StringRequest stringRequest=new StringRequest(Request.Method.POST, login_url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            responseServer.setText("Successfull");

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String e=error.toString();
                    responseServer.setText(e);
                    error.printStackTrace();

                }
            })
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params=new HashMap<String, String>();
                    params.put("user_id",ID);
                    params.put("user_password",password);
                    return params;
                }
            };




            MySingleton.getInstance(getApplicationContext()).addToRequestque(stringRequest);
            //AppController.getInstance().addToRequestQueue(stringRequest);
        }

    }

这里是 MySingleton 类 包 com.example.schoolmanagementsystem;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class MySingleton {

    private static MySingleton mInstance;
    private static RequestQueue requestQueue;
    private static Context context;

    public MySingleton(Context mCx) {
        context=mCx;
        requestQueue=getRequestQueue();
    }

    public RequestQueue getRequestQueue() {

        if(requestQueue==null){
            requestQueue= Volley.newRequestQueue(context.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized MySingleton getInstance(Context context){
        if (mInstance==null){
            mInstance=new MySingleton(context);
        }
        return mInstance;
    }

    public <T>void addToRequestque(Request<T> request){
        requestQueue.add(request);
    }
}

我正在使用 android 3.3.2 和 android pie 来运行它

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    使用重试策略:

    stringrequest.setRetryPolicy(new DefaultRetryPolicy(3000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    // Adding the request to queue
    BaseController.getInstance().addToRequestQueue(jsObjRequest);
    

    【讨论】:

      【解决方案2】:

      在请求之前添加此代码

      int socketTimeout = 30000;
              RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
              stringRequest.setRetryPolicy(policy);
      MySingleton.getInstance(getApplicationContext()).addToRequestque(stringRequest);
      

      针对 Android PIE 9.0 中的网络问题 按照这个 Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

      【讨论】:

      • 并确保此 login_url 正常工作,在邮递员或其他客户端上进行测试
      • 不工作兄弟,即使我将它增加到 100000 ,我的网址是正确的,我在浏览器中测试它
      • 请把logcat贴在这里
      【解决方案3】:

      使用这样的代码对我有用

      request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
      
      mQueue.add(request);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 2019-06-25
        相关资源
        最近更新 更多