I have to make json array request with the following request paramatetrs.
[
"Login",
{
"password": "",
"username": "",
"ip": "12.123.124.12",
"login_type": "Android"
}
]
I use volley to make post request. In volley, if we have to make jsonarrayrequest, we do something like the following,
JsonArrayRequest req = new JsonArrayRequest(Constants.requestUrl,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
The problem is that, How can I ever be able to insert my request parameters in the above code snippet. In case of JsonObjectRequest, we have provision to insert as follows,
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.requestUrl, frameLoginJson(),
new Response.
Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
In frameLoginJson, im framing the request parameter and dispatching the request.
But Im unable to do the same in case of JSONArrayRequest. How can I be able to make json array request with request parameters using volley especially or by any other mean?
You would need to override the getParams method with the following signature: protected Map<String,String> getParams().
Here's an example:
see the answer to Volley not calling getParams() for standard POST request
Related
CODE
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjectRequest);
PROBLEM
This is my code in which I'm using the Volly-Library to get the JSON Object using the GET Method. I'm using Volly Library in my Android App to get the Json response using the Volly-Library. But it is throwing the AuthFailureError.
URL: http://androidtutorialpoint.com/api/volleyJsonObject
Code:
public void volleyJsonObjectRequest(String url) {
String REQUEST_TAG = "JSONOBJ_TAG";
JsonObjectRequest jsonObjectReq = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY RESPONSE", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
VolleyLog.d("VOLLEY ERROR", "Error: " + error.getMessage());
}
});
// Adding JsonObject request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);
}
I am trying to log the volley respone but I am getting the following error instead:
Cannot cast 'com.android.volley.ServerError' to 'com.android.volley.NoConnectionError'
I have checked the URL in the POSTMAN and it works fine. Is there something I am missing in my code? Been trying to debug but couldn't find the root cause.
Found the solution:
Replace http with https
I've got class with simple Json Object Request, this is method where whole request is called, and in LogCat I get only:
Volley: [2] 2.onErrorResponse: Error:
so I don't know where to look for a fix
private void getBeerDetails() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET,
"https://api.punkapi.com/v2/beers/13",
null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
beerName.setText(response.getString("name"));
alc.setText(response.getString("abv"));
ibu.setText(response.getString("ibu"));
firstBrewed.setText(response.getString("first_brewed"));
yeast.setText(response.getString("yeast"));
description.setText(response.getString("description"));
foodPairing.setText(response.getString("food_pairing"));
Picasso.with(getApplicationContext())
.load(response.getString("image_url"))
.into(beerImageView);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error:", error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
As I see here your response is in JsonArray format.
So try with below
private void getBeerDetails() {
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
(Request.Method.GET,
"https://api.punkapi.com/v2/beers/13",
null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
if(response.length()>0){
//make a loop and add item to your list
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error:", error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
Your error message is not very descriptive. However, I think you need to set the content-type to application/json in the header for your GET request. Override the getHeaders function while creating the request. Here's a java example.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
Update
As per your comment in the answer, you have failed to parse the JSON which was returned from your GET request. As I have seen from your response JSON, you are receiving an array of objects. I would like to suggest using Gson for JSON parsing. Its simple and easier to implement. You need to define a class containing the fields of the object in your array which is returned in your response. Then just use Gson to convert the values from JSON array into the array of that specific object that you have created. Here's a sample.
Gson gson = new Gson();
Data[] dataArray = gson.fromJson(jsonLine, Data[].class);
I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?
till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}
Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Well long story short i face this kind of problem, Im trying to send volley request to my server, which responds with json array:
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
but no matter what i tried i still get Volley response error, my volley request:
requestQueue = Volley.newRequestQueue(getActivity());
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(MY_URL ,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonArrayRequest);
the weird thing that with volley string request it does work, only the array mess things for me.
EDIT:
how ever this way i do get my array using feed:
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_INDEX + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
Thank you!
Use this code, Change JsonArrayRequest to JsonObjectRequest
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
Array Response
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
Object Response
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}