如何修复Android中的Volley超时错误

时间:2019-03-18 11:51:41

标签: android android-volley

当我尝试运行我的代码时,它给我那个有问题的错误 首先,它给我一个不允许使用明文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运行此

3 个答案:

答案 0 :(得分:0)

使用重试策略。

stringrequest.setRetryPolicy(new DefaultRetryPolicy(
    3000, 
    0, 
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

// Adding the request to queue

BaseController.getInstance().addToRequestQueue(jsObjRequest);

答案 1 :(得分:0)

在请求之前添加此代码

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)

答案 2 :(得分:0)

使用像这样对我有用的代码

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

mQueue.add(request);