2

When I try to run my code it gives me that error which is in question first, it gives me an error of cleartext HTTP is not permitted then I try to fix it by adding this line to manifest file android:usesCleartextTraffic="true" after this, its give me a timeout error I also try these to fix it click here but it's not working here is my code

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);
        }

    }

here is MySingleton class package 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);
    }
}

I am using android 3.3.2 and android pie to run this

Nilesh Panchal
  • 1,059
  • 1
  • 10
  • 24
Learn Do
  • 41
  • 1
  • 5

3 Answers3

1

Use Retry Policy:

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

// Adding the request to queue
BaseController.getInstance().addToRequestQueue(jsObjRequest);
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Gopal Reddy
  • 36
  • 1
  • 5
1

Use code like this it is working for me

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

mQueue.add(request);
David Buck
  • 3,752
  • 35
  • 31
  • 35
Amit Joshi
  • 35
  • 9
0

Add this code before requesting

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

For Network issue in Android PIE 9.0 follow this Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

Quick learner
  • 10,632
  • 4
  • 45
  • 55