getting new data from a json file using volley - android

I have an android java app and i need some data from a json file so i used volley to get data from json
my problem is when i change data on the json file i should uninstall the app and install it again then get the new data otherwise it keeps the old data even after killing it from ram
RequestQueue requestQueue = Volley.newRequestQueue(this);
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, LINK, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArrayVariabls = response.getJSONArray("variables");
JSONObject var = jsonArrayVariabls.getJSONObject(0);
Variables.name = var.getString("name");
Variables.adresse=var.getString("adresse");
callBack.onSuccess();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);

The problem was from cache so you need to clear it before request using this line
requestQueue.getCache().clear();

Related

JSONObject response null

I'm trying to send a simply JSON request using Volley JsonObjectRequest.
After getting the JSON response, I would like to update a value called valoreConvertito, but the JSON response is null and consequently valoreConvertito remains zero.
private void convertiREST(final Double valoreDaConvertire, String valuta){
final TextView textView = (TextView) findViewById(R.id.text);
RequestQueue queue = Volley.newRequestQueue(this);
String url =COMPLETEURL;
valoreConvertito = 0.0;
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.e("Rest response ", response.toString());
valoreConvertito = response.getJSONObject("quotes").getDouble("valuta");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest response", error.toString());
}
});
queue.add(objectRequest);
}
I've even followed the suggestions in another post (Volley JsonObjectRequest response) but I still got JSON response null.
Using the debugger it seems that the program doesn't enter neither in onResponse nor in ErrorListener.
After adding the row queue.add(objectRequest); I notice in Logcat the HTTP traffic not permitted error and I've solved my issue following Android 8: Cleartext HTTP traffic not permitted post.

How to store data requested by android volley to make some processing on data out of on response?

I want to store data requested by android volley to make some processing on data outside the onResponse ,I tried shared preference but it not updated if i have several volley requests.
this.context=context;
//"https://apifootball.com/api/?action=get_H2H&firstTeam=Arsenal&secondTeam=Chelsea&APIkey=**********"
String URL="https://apifootball.com/api/?action=get_H2H&firstTeam="+team1+"&secondTeam="+team2+"&APIkey=****************8";
//"https://apifootball.com/api/?action=get_countries&APIkey=*********";
RequestQueue rq= Volley.newRequestQueue(context);
JsonObjectRequest objreq= new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
String Scores="";
// Log.e("result:",response.get(0).toString());
JSONObject obj;
// obj=response.getJSONObject("firstTeam_VS_secondTeam");
try {
JSONArray obj2 =response.getJSONArray("firstTeam_VS_secondTeam");
Log.e("obj", obj2.getJSONObject(0).getString("match_hometeam_score"));
Scores=Scores+ obj2.getJSONObject(0).getString("match_hometeam_score")+"\n"+obj2.getJSONObject(0).getString("match_awayteam_score")+"\n"+obj2.getJSONObject(0).getString("match_date");
} catch (JSONException e) {
}
share(Scores);
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Log.e("rest response",error.toString());
}
}
);
rq.add(objreq);
SharedPreferences m=PreferenceManager.getDefaultSharedPreferences(context);
final String resp=m.getString("Response","");
Log.e("Res","x");
return resp;

Volley Error on Api calling

Am trying to fetch a result from an api using volley using the following snipet
Set<String> tags= new HashSet<String>() ;
tags.add("geethu");
tags.add("gautham");
tags.add("gauri");
JSONObject deviceInfo = new JSONObject();
deviceInfo.put("Platform", "gcm");
deviceInfo.put("Handle", "handle");
deviceInfo.put("Tags", new JSONArray(tags));
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, "http://172........", deviceInfo,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.e("Please output",jsonObject.toString());
}
}, new Response.ErrorListener (){
#Override
public void onErrorResponse(VolleyError volleyError) {
// Log.d(App.TAG,volleyError.toString());
}
}
);
jsonObjectRequest.setRetryPolicy(new
DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);
} catch (JSONException e) {
e.printStackTrace();
}
and am getting an output as
[8361] BasicNetwork.performRequest: Unexpected response code 500 for http://domian/..........
plz help me to find out what I am doing wrong

Android application Online and offline

I have to create an android application which shows some reports and images in Recycler view using json volley request it works when internet is available i also need to show the once loaded data in recycler view when internet is not available here is the code i used to select data when internet is available
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, Config.DATA_URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String str = response.toString();
loading.hide();
JSONArray arr = response.getJSONArray("Stock_Report");
parseData(arr);
} catch (JSONException e) {
e.printStackTrace();
}
// now you have the array. in this array you have the jsonObjects
// iterate on this array using a for loop and parse like you did before
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
//Adding request to the queue
requestQueue.add(jsObjRequest);
}
please help me to find better solution to show this data when internet is not available also thanks in advance

com.android.volley.ParseError: org.json.JSONException

I got this error from volley library
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
the error
com.android.volley.ParseError: org.json.JSONException: Value [{"id":"admin","name":"Admin"}] of type org.json.JSONArray cannot be converted to JSONObject
How can I receive the result as string and then I will process it using jackson ?
If you want to receive the result as a string don't use the JSONRequest. Go with the simple Request class.
Your problem is pretty simple the server is giving back a JSONArray with just one element inside.
A JSONArray is not a JSONObject. That's why the parsing is failing.
We Have to use JsonArrayRequest instead of JsonObjectRequest. The code as:
RequestQueue queue = Volley.newRequestQueue(this);
final String url = "http://192.168.88.253/mybazar/get_product_list.php";
// prepare the Request
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>()
{
#Override
public void onResponse(JSONArray response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Hope, it's solve the problem.
I noticed that there is class JsonArrayRequest supported by volley so I use this class and the problem solved, I was using JsonObjectRequest
https://android.googlesource.com/platform/frameworks/volley/+/43950676303ff68b23a8b469d6a534ccd1e08cfc/src/com/android/volley/toolbox
Probably the below logic will work for you:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject1 = new JSONObject(response.toString());
JSONArray jsonArray = jsonObject1.getJSONArray("statewise");
Log.d("Json response", "onResponse: "+jsonObject1.toString());
for (int i = 0; i < jsonArray.length; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//Here you will get your result so can use textview
//to populate the result
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: "+error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}

Categories

Resources