i am developing an application that request data from MYSQL
i used an API (json) to retrieve the data from database.
My problem is that my project works fine on android studio but when i am running it on real device (Samsung GT-I9082) i got an exception and i don't know why!
the exception is:
java.io.EOFexception
mycode:
private void getData(String leng) {
final String id="23119574";
try {
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);
// String url = config.DATA_URL3+id;
String url = config.DATA_URL4 + id;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response, id);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Handler_Barcode.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
stringRequest.setRetryPolicy((new DefaultRetryPolicy(
20000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)));
requestQueue.add(stringRequest);
}catch(Exception e){
e.printStackTrace();
}
}
note: i am using volley
and my device android version is 4.2.2
thank you!
Related
I am a new guy in android and trying to make a rest api call using volley.
I am getting "com.android.volley.ClientError" in my code. Can anyone help me to solve this please.
private void LoginByNet(String uID, String pSD){
String URL = "http://myipaddress:65017/api/values";
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jq = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Success:"+response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error:"+ error.toString(), Toast.LENGTH_SHORT).show();
userName.setText(error.toString());
}
}
);
rq.add(jq);
}
This means that the server returned a 4xx error code.
[https://github.com/google/volley/blob/d1a3d5388c79ff1a6fdb904daeff9b39d0bb7d26/src/main/java/com/android/volley/toolbox/BasicNetwork.java#L199][1]
You can get the exact error code using : #error.networkResponse.statusCode
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
I am sending a request to server and it causes errors. I was able to trace the error to the fact that Volley is sending the request more than once. I searched the internet for solutions, I tried all what I came across but none of them seem to solve the problem
Below is my code:
public void btnLogOut(View view) {
final ProgressDialog loading = ProgressDialog.show(this, "Logging Out", "Please wait...", false, false);
//cover.setVisibility(View.GONE);
String token = dbHelper.getAuth().getString(0);
String IP = helperFunctions.getAppUrl();
final String url = IP + "/deregister?token=" + token+ "&appVersion=" + versionCode;
JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, url,null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
loading.dismiss();
logOut.LogOutUser();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (VolleyErrorHelper.getMessage(error, Settings.this).equalsIgnoreCase("401")){
logOut.MakeUserLogin();
}else{
cover.setVisibility(View.VISIBLE);
}
//VolleyLog.e("Deregister GCM", "Error: " + error.getMessage());
loading.dismiss();
Toast.makeText(Settings.this, "Process not completed, try again!", Toast.LENGTH_LONG).show();;
}
});
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(sr);
}
Try this:
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));
I used on the POST requests so it can do a retry
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();
}
});
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);