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