0

First thing is, I am new to android programming. I was trying to send data to a PHP server using volley library by watching a video through youtube. But whenever I am clicking the register button, getting an error "com.android.volley.timeouterror". Please help me. This is my code...

    StringRequest stringRequest= new StringRequest(Request.Method.POST, ip, new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONArray jsonArray=new JSONArray(response);
                                            JSONObject jsonObject=jsonArray.getJSONObject(0);
                                            String code=jsonObject.getString("code");// used in php file
                                            String message=jsonObject.getString("message");
                                            builder.setTitle("Server Response");
                                            builder.setMessage(message);   //message from server
                                            displayAlert(code);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }}
                                }, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Toast.makeText(first.this, "Call admin error in query"+error, Toast.LENGTH_SHORT).show();
                                    }
                                })//passing data to string request getparams method
                                 {
                                      @Override
                                     protected Map<String, String> getParams() throws AuthFailureError {
                                         HashMap<String, String> params= new HashMap<>();
                                         params.put("name",name);
                                         params.put("email",email);
                                         params.put("password",password);
                                         return params;
                                     }};
                                //string request to requestqueue
                                MySingleton.getInstance(first.this).addToRequestqueue(stringRequest);

                            } else {
                                builder.setTitle("Something went wrong!");
                                builder.setMessage("password doesn't match");
                                displayAlert("input_error");
                            }
                        }
            }

    });

}
public void displayAlert(final String code){
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (code.equals("input_error")){
                jet3.setText("");
                jet4.setText("");
            }
            else {
                if(code.equals("Reg_Success")){
                    finish();
                }
                else if(code.equals("Process_Failed")){
                    jet1.setText("");
                    jet2.setText("");
                    jet3.setText("");
                    jet4.setText("");
                }
            }
        }
    });
    AlertDialog alertDialog= builder.create();
    alertDialog.show();
}

And file mysingleton.java

public class MySingleton {
private static MySingleton mInstance;
 private RequestQueue requestQueue;
 private static Context mCtx;
 //create a constructor
 private MySingleton(Context context){
     mCtx=context; //initialize context
     requestQueue=getRequestQueue(); //initialize request queue
 }
 public RequestQueue getRequestQueue(){
     if(requestQueue==null){
         requestQueue= Volley.newRequestQueue(mCtx.getApplicationContext());
     }
     return requestQueue;
}
//return instance of class
public static synchronized MySingleton getInstance(Context context){
     //initialize mInstance if it is null
     if (mInstance==null){
         mInstance=new MySingleton(context);
     }
     return mInstance;
}
//adding request to requestqueue
public <T>void addToRequestqueue(Request<T> request){
    requestQueue.add(request);
}

}

  • [1](https://stackoverflow.com/questions/25994514/volley-timeout-error) Make sure to read all the answers and see if that works – denvercoder9 Mar 04 '18 at 16:03

1 Answers1

0

use the following code ` // your time out time extend it

 private static final int MY_SOCKET_TIMEOUT_MS = 10000;

 MySingleton.getRequestQueue().setRetryPolicy(new DefaultRetryPolicy(
    MY_SOCKET_TIMEOUT_MS, 
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
hamza khan
  • 141
  • 1
  • 11
  • Where to put this code, it is showing error cannot find symbol getRequestQueue if I put it in the first activity. –  Mar 05 '18 at 14:00
  • MySingleton.getInstance.getRequestQueue().setRetryPolicy(new DefaultRetryPolicy( MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); – hamza khan Mar 05 '18 at 14:05
  • get your sigleton instance by MySingleton.getInstance.getRequestQueue() – hamza khan Mar 05 '18 at 14:07
  • https://stackoverflow.com/questions/17094718/change-volley-timeout-duration?rq=1 check this link – hamza khan Mar 05 '18 at 14:09
  • cannot find symbol setRetryPolicy –  Mar 06 '18 at 07:18
  • stringRequest.setRetryPolicy(new DefaultRetryPolicy( MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); – hamza khan Mar 06 '18 at 12:59
  • try like this . – hamza khan Mar 06 '18 at 12:59
  • Thank u for your help but the error keeps coming 'com.android.volley.timeouterror' even I changed time 10000 to 30000. –  Mar 06 '18 at 15:09