The following is my code. I get com.android.volley.NoConnectionError: java.io.InterruptedIOException for the first time and for the second time it works fine. There server response is also fine, No error in server side.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest request = new JsonObjectRequest(URL, null,
new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject responseJsonObject) {
try {
if (responseJsonObject.has("user")
&& !responseJsonObject
.isNull("user")) {
user.jsonParser(responseJsonObject
.getJSONObject("user"));
}
} catch (JSONException exception) {
Toast.makeText(context, "Error Occured",
Toast.LENGTH_SHORT).show();
}
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
progressDialog.dismiss();
Log.d("Volley Error", volleyError.toString());
Toast.makeText(context, "Connectivity Error",
Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
progressDialog.show();
queue.start();
I had the same problem and I solved it by removing the following line
queue.start()
I was having the same issue. The question Vamsi referenced has a workaround using OkHttp that you works if you use OkHttp as the stack in volley (see also the comments in that question on how to use OkHttp in volley).
However, here is another hacky workaround that doesn't involve using another library:
final RequestQueue requestQueue = getRequestQueue();
final class ErrorFirstTimeHack {
boolean first_time = true;
MyVolleyJsonObjectRequest request = null;
}
final ErrorFirstTimeHack errorFirstTimeHack = new ErrorFirstTimeHack();
final MyVolleyJsonObjectRequest request = buildRequestObject(
Request.Method.GET,
url,
new Response.Listener() {
#Override
public void onResponse(Object response) {
// normal success processing
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error instanceof com.android.volley.NoConnectionError) {
if (errorFirstTimeHack.first_time) {
errorFirstTimeHack.first_time = false;
requestQueue.add(errorFirstTimeHack.request);
return;
}
}
// normal error processing
}
}
);
errorFirstTimeHack.request = request;
requestQueue.add(request);
Related
I am new to firebase. I tried to store user data in firebase database using volley. However, firebase has no response regarding my volley request and the database still is null. This is the tutorial I followed.
This is the volley request I used to connect firebase.
public void executeFirebase(){
StringRequest request = new StringRequest(Request.Method.GET, FIREBASE_REGISTER_URL, new Response.Listener<String>(){
#Override
public void onResponse(String s) {
Firebase reference = new Firebase("https://tradeal-930ad.firebaseio.com/users");
if(s.equals("null")) {
reference.child(name).child("password").setValue(password);
Toast.makeText(activity, "registration successful 1", Toast.LENGTH_LONG).show();
}
else {
try {
JSONObject obj = new JSONObject(s);
if (!obj.has(name)) {
reference.child(name).child("password").setValue(password);
Toast.makeText(activity, "registration successful 2", Toast.LENGTH_LONG).show();
Intent intent = new Intent(activity, MainPageActivity.class);
activity.startActivity(intent);
activity.finish();
loginUserActivity.finish();
} else {
Toast.makeText(activity, "username already exists", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
loading.dismiss();
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError );
loading.dismiss();
}
});
RequestQueue rQueue = Volley.newRequestQueue(activity);
rQueue.add(request);
}
change the rules of your database to public
{
"rules" :
{
".read" : true,
".write" : true
}
}
otherwise
{
"error" : "Permission denied"
}
this will be your response
to know more about rules you can visit security rules
So I've run into a problem in my code, where I need to get a JSONString from my server with volley. And then I have to parse the String into a JSONObject and then continue doing stuff with that.
My problem here is, that Volley gives the response too late, meaning my string that I want to parse is always empty because its not initialised yet.
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest stringRequest = new StringRequest(Request.Method.GET, searchURLBuilder, future, future);
// new Response.Listener<String>() {
// #Override
// public void onResponse(String response) {
// writeToSharedResponse(response_for_search, response);
// }
// },
// new Response.ErrorListener() {
// #Override
// public void onErrorResponse(VolleyError error) {
// Log.d("Error!!:" + error.getMessage(), "");
// }
// });
requestQueue.add(stringRequest);
String response = "";
try {
response = future.get(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
String responseString = m.getString(response_for_search, new String());
pieceDTOList = getPiecesDTOFromJSON(responseString);
Here is a snippet of my code. As you can see I already tried to make a "future" call to block and wait for the answer, but it just times out every time. The commented out bit, is the part I actually wanted to use from the beginning, but that returns the response to late. Since its asynchronous and accesses the server w/e it wants.
writeToSharedResponse just writes the answer into a sharedPreferences variable.
private SharedPreferences m;
private SharedPreferences.Editor editor;
private RequestQueue requestQueue;
public DbParser(Context c) {
m = PreferenceManager.getDefaultSharedPreferences(c);
editor = m.edit();
requestQueue = Volley.newRequestQueue(c);
}
My Question here is: Is there an easy way I can "wait" for the answer from volley so I can continue to work with the response that I get?
Edit 1:
I now added an interface and changed the code around to this:
getString(new VolleyCallback() {
#Override
public void onSuccess(String result) {
getPiecesDTOFromJSON(result);
}
}, searchURLBuilder);
return globalPieceDTOList;
}
private void getString(final VolleyCallback callback, String searchUrl){
StringRequest stringRequest = new StringRequest(Request.Method.GET, searchUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
callback.onSuccess(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error!!:" + error.getMessage(), "");
}
});
requestQueue.add(stringRequest);
}
Since I cant get the values out of my inner classes, I did a nasty hack and created a global list for my DTO's. The problem now is that "return globalPieceDTOList" is always Null. And again - I would need to "wait" for the Volley response.
Volley requests are async, so when you try to return a value, the request is likely not done yet. If you want to return values from a Volley request use callback interfaces.
Example
public void getString(final VolleyCallback callback) {
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// do some things here
callback.onSuccess(<PASS STRING RESULT HERE>);
}
});
}
public interface VolleyCallback{
void onSuccess(String result);
}
Example usage:
public void onResume(){
super.onResume();
getString(new VolleyCallback(){
#Override
public void onSuccess(String result){
//do stuff here with the result from the volley request
}
});
}
I have a function getTeacherData()
and it returns a json object,The problem is when i disable my internet connection on my phone the app crashes(android doesn't show the error in android monitor,it just crashes without showing the bug),
When i run the same app on my emulator it works fine and if i disconnect my internet the onErrorResponse is also working ,but it crashes when i run it on other phones
private void getTeacherData() {
if (username.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
String url = DATA_URL+username;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
try {
JSONObject jsonObject =new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
for(int x=0;x<result.length();x++) {
JSONObject collegeData = result.getJSONObject(x);
albumList.add(new Album(collegeData.getString(config.course_name),collegeData.getString(config.semester)
,collegeData.getString(config.section),collegeData.getString(config.total_classes)
,collegeData.getString(config.subject),collegeData.getString(config.classes_taken),collegeData.getString(config.subject_code)));
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
things i have tried :
Removed showJSON(); method inside onResponse().
Surrounded the string request with try catch.
Surrounded whole getTeacherData() with try catch.
It seems like onErrorResponse responds differently on few devices
onErrorResponse returned null on few devices (that was the reason for the crash)
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if(error.getMessage==NULL){
Toast.makeText(cardview.this, "Failed to retrieve data", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
I want to send a POST request to my server, and there is no expected data for the result (just HTTP status code - standard behavior). How can I do that ?
(abstract base Request class (Volley) wants a result type)
try {
mRequest =
new XXXXXX(
Request.Method.POST,
url,
null, null,
new Response.Listener() {
#Override
public void onResponse() {
// ok
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
// ko
}
},
null
);
return mRestCoreVolley.addToRequestQueue(mRequest);
} catch (Exception e) {
// error
}
You could try something like in the code below for the response listener:
new Response.Listener<Void>() {
#Override
public void onResponse(Void response) {
}
}
I guess your code is right. You can use a String like:
RequestQueue rq = Volley.newRequestQueue(this);
StringRequest postReq = new StringRequest(Request.Method.POST,
your_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// do nothing
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
}) ;
Hope it helps you!
I want to pass a response outside of my classes (many classes)
public static void userLocation()
{
RequestQueue queue = Volley.newRequestQueue(context);
String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";
StringRequest dr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
//Toast.makeText(context, ""+response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error.
// Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
Log.d("error", ""+error);
}
}
);
queue.add(dr);
}
Please suggest how I can pass a response outside of the class
In volly its difficult to return response outside the class.because request on server run in background and if we return value followed by queue.add(url) then it will return null.So there is no solution till now.Thanks!!