JsonArrayRequest not working onResponse method in Android Volley - android

Hello friends I have following JSON Array response from a web service and I want to read all the data row wise from the Database and display on Android Activity.
[{"id":"2","type":"0","title":"Recruitment at ADANI Port, Mundra","date":"2016-07-01"},{"id":"1","type":"1","title":"Training at DAICT, Gandhinager","date":"2016-07-04"}]
I have implemented following code in onCreate() of an Activity using Volley Library, which doesn't show output and to my knowledge it doesn't call onResponse() method.
public void getNews(){
String url = "http://www.ABCDXYZ.net/tponews.php?tponews=1";
Log.e("URL",url);
requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jor = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
Log.e("Resposne", "We have got the response");
JSONObject val = response.getJSONObject(0);
}catch (JSONException e){e.printStackTrace();}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley",error.toString());
}
}
);
requestQueue.add(jor);
}
The output is only URL in Log.e("URL",url); // URL: www.ABCXYZ.net/tponews.php?tponews=1

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;

JsonArrayRequest using Android Volley library (POST request)

Iam using Volley library to post json array to RestApi. But i get an error BasicNetwork.performRequest: Unexpected response code 400. I lookfor many articles Volley - Sending a POST request using JSONArrayRequest but not get any solution yet.
Here's my code,
final JSONArray jsonArray = new JSONArray();
List<User> users = UserManager.composeUsers();
jsonArray.put(users);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, "http://192.168.137.1:8080/create", jsonArray, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("Response: ", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error: ", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return String.format("application/json; charset=utf-8");
}
};
queue.add(jsonArrayRequest);
}
In the above code List<User> user return a java object. I want to pass the user object to rest service.

How do I get JSON array list from the Server?

I am using Volley in my android client to retrieve some JSON values from server.
To get Value I did the following :
#Override
public void onClick(View v) {
queue = Volley.newRequestQueue(MapsActivity.this);
request = new JsonObjectRequest(
Request.Method.GET, // the request method
"http://10.0.2.2:8080/SomeURL",
new JSONObject(map),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response){
textView = (TextView)findViewById(R.id.textView);
textView.setText(response.toString());
}
},
new Response.ErrorListener() { // the error listener
#Override
public void onErrorResponse(VolleyError error) {
textView = (TextView)findViewById(R.id.textView);
textView.setText("Error::"+ error.toString());
Toast.makeText(getApplicationContext(), "Error:" + error.toString(), Toast.LENGTH_LONG).show();
}
});
queue.add(request);
}
});
And In server side I am returning the value as following:
#GET
#Produces("application/json")
public List<Employee> getEmployees() {
EmployeeDAO dao = new EmployeeDAO();
List employees = dao.getEmployees();
return employees;
}
However, I am getting the values in "onErrorResponse()"
You are waiting for a JSONObject in your code, change JsonObjectRequest with JsonArrayrequest. In onResponse(JSONArray response) you parse the response

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