Response handling with Volley - android

I am using Volleyin my project for handling network requests. Here is a sample JSON my server returns
JSON Object Response
{"code":"success", "data":{"some data"}}
JSON Array Response
{"code":"success", "data":["some data"]}
When some validation error or any other error occurs, server returns following response:
{"code":"failed", "error":"Access denied"}
The problem is with parsing data. when request is successful, in onResponse of ResponseListener, I simply get the content of data key. Where as, I was expecting the result same as what I posted above. I am not getting why Volley is returning only content of data and not complete JSON. I had used Volley earlier also. But never faced such type of problem.
Parsing Code:
private void getOnboardingCategories() {
Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(LOG_TAG, "CATEGORY RESPONSE: " + response.toString());
if (response != null) {
int dataLength = response.length();
for (int i = 0; i < dataLength; i++) {
JSONObject jObject = response.optJSONObject(i);
if (jObject != null) {
CategoryType2 categoryType2 = new CategoryType2();
categoryType2.set_id(jObject.optString("_id"));
categoryType2.setName(jObject.optString("name"));
categoryType2.setApp_icon_data(jObject.optString("thumbnail_data"));
categories.add(categoryType2);
}
}
}
if (isVisible())
sellAdapter.notifyDataSetChanged();
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Util.errorHandler(error, ctx);
}
};
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Method.GET, url,
null, responseListener, errorListener);
MyApplication.getInstance().addToRequestQueue(jsonObjectRequest, "onboarding");
}
Response on Success:
{
code: "success",
data: [
{
_id: "55c06b05a3e0041a73cea744",
name: "Test Category 1",
thumbnail_data: "",
},
{
_id: "55c06b16a3e0046108cea744",
name: "Test Category 2",
thumbnail_data: "",
}
]
}
In onResponse of ResponseListener, I get this data:
[
{
_id: "55c06b05a3e0041a73cea744",
name: "Test Category 1",
thumbnail_data: "",
},
{
_id: "55c06b16a3e0046108cea744",
name: "Test Category 2",
thumbnail_data: "",
}
]
When error occurs, server returns this response:
{"code":"failed", "error":"error_msg"}
Due to this, Volley throws ParseException as it expects JSONArray. I need to show the error message to the user. Earlier, I was using AsyncTask and I handled the error there. But, with Volley I am facing difficulty. I looked into VolleyError, but didn't got any clue.
Update 1
private void getOnboardingCategories() {
showSpinnerDialog(true);
Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(LOG_TAG, "CATEGORY RESPONSE: " + response.toString());
hideSpinnerDialog();
String code = response.optString("code");
if (code.equals("success")) {
if (response != null) {
JSONArray dataArray = response.optJSONArray("data");
int dataLength = dataArray.length();
for (int i = 0; i < dataLength; i++) {
JSONObject jObject = dataArray.optJSONObject(i);
if (jObject != null) {
CategoryType2 categoryType2 = new CategoryType2();
categoryType2.set_id(jObject.optString("_id"));
categoryType2.setName(jObject.optString("name"));
categoryType2.setApp_icon_data(jObject.optString("app_icon_data"));
categories.add(categoryType2);
}
}
}
}
if (isVisible())
sellAdapter.notifyDataSetChanged();
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Util.errorHandler(error, ctx);
}
};
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET, url,
null, responseListener, errorListener);
MyApplication.getInstance().addToRequestQueue(jsonObjectRequest, "onboarding");
}
Update
This issue was not about Volley. There was issue on the server end wrt gzip compression. I am going to vote for closing this question.

But, when error occurs, I get Parse exception, when making request
for JSONArray
Use JSONObject. has() and JSONObject. isNull() to check which key is present in json response before parsing json.
For Example:
JSONObject jsonObject=new JSONObject(<server_response_string>);
if(jsonObject.has("data") && !jsonObject.isNull("data"))
{
// get data JSONArray from response
}else{
// get message using error key
}

An efficient method to handle this kinda situation can be achieved through parsing JSON values using GSON and assign the key values using POJO class.
Example:
Add error scenario in both the cases like handling JSONArray or JSONObject. Please find the samples of your required POJO for your test data as follows.
Sample 1
public class JSONArrayPojo
{
private ArrayList<String> data;
private String code;
private String error;
public String getError() {
return this.error;
}
public void setError(String value) {
this.error = value;
}
public ArrayList<String> getData ()
{
return data;
}
public void setData (ArrayList<String> data)
{
this.data = data;
}
public String getCode ()
{
return code;
}
public void setCode (String code)
{
this.code = code;
}
}
Sample 2
public class JSONObjectPojo
{
private String data;
private String code;
private String error;
public String getError() {
return this.error;
}
public void setError(String value) {
this.error = value;
}
public String getData ()
{
return data;
}
public void setData (String data)
{
this.data = data;
}
public String getCode ()
{
return code;
}
public void setCode (String code)
{
this.code = code;
}
}
Generating GSON from your response and handling out the both positive(success) and negativ(error) scenario as follows:
#Override
public void onResponse(JSONArray response) {
Log.d(LOG_TAG, "CCMP CATEGORY RESPONSE: " + response.toString());
if (response != null) {
//converting JSON response into GSON Format
JSONArraryPojo jsonArray = null;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
Gson gson = gsonBuilder.create();
jsonArray = gson.fromJson(response.toString(), JSONArraryPojo.class);
if(jsonArray.getCode().equals("success")){
//process your steps if the value is success
Toast.makeText(this, jsonArray.getCode(), Toast.LENGTH_SHORT).show();
}else {
//displaying toast when error occurs
Toast.makeText(this, jsonArray.getError(), Toast.LENGTH_SHORT).show();
}
}
}
};
Reference links to parse into GSON from JSON
http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON
http://examples.javacodegeeks.com/core-java/json/json-parsing-with-gson/
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
http://www.mysamplecode.com/2013/07/android-json-stream-data-parsing.html
http://blog.nkdroidsolutions.com/
Note: To make use of GSON library in android.
Add following lines in gradle:
compile 'org.immutables:gson:2.1.0.alpha'

In case of error your server should return error json with http status code 4xx. That is how you design Restful APIs. In current situation, your API is always returning 2xx which corresponds to successful API result.
If your API sends correct http response code, in this case 401 (unauthorized) or 403 (forbidden) refer here then your ErrorListener will be called by Volley. You don't have to write parsing logic for error response in ResponseListener. Here is a good resource for understanding rest api http status codes.

UPDATE RESULT SCREENSHOTS:
Success case: JSONArray
[
{
"_id": "55c06b05a3e0041a73cea744",
"name": "Category 1",
"thumbnail_data": ""
},
{
"_id": "55c06b16a3e0046108cea744",
"name": "Category 2",
"thumbnail_data": ""
}
]
Error case: JSONObject
{
"code": "failed",
"error": "error_msg"
}
In my code below, pay attention to parseNetworkResponse.
The following is my updated answer, I have tested for both responses you provided:
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
if (!response.isNull("success")) {
JSONArray jsonArray = response.getJSONArray("success");
Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show();
if (mTextView != null) {
mTextView.setText(jsonArray.toString(5));
}
} else {
String codeValue = response.getString("code");
if ("failed".equals(codeValue)) {
String errorMessage = response.getString("error");
Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show();
if (mTextView != null) {
mTextView.setText("Error Message:\n\n" + errorMessage);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
// Check if it is JSONObject or JSONArray
Object json = new JSONTokener(jsonString).nextValue();
JSONObject jsonObject = new JSONObject();
if (json instanceof JSONObject) {
jsonObject = (JSONObject) json;
} else if (json instanceof JSONArray) {
jsonObject.put("success", json);
} else {
String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}";
jsonObject = new JSONObject(message);
}
return Response.success(jsonObject,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
};
queue.add(jsonObjectRequest);
Hope this helps!

You can check the value of code key as it will always be available to you whether the response would be a failure or success. Below is a small snippet:
JSONObject jObj = new JSONObject(your_response_string);
if(jObj.getString("code").equalsIgnoreCase("failed"))
{
//write code for failure....
}
else{
//write code for success......
}
Note: A more modular way to do this is to make a model class and set your values in it. This way you will get all your values in a single java object.

Maybe you can try this:
Use the fastjson library to convert your json string to your java object in Response.Listener like this:
Pojo pojo = JSON.parseObject(response.toString(), Pojo.class);
And your Pojo may like this:
public class Pojo {
private String code;
private String error;
private List<Date> data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public List<Date> getData() {
return data;
}
public void setData(List<Date> data) {
this.data = data;
}
public class Data {
public String _id;
public String name;
public String thumbnail_data;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getThumbnail_data() {
return thumbnail_data;
}
public void setThumbnail_data(String thumbnail_data) {
this.thumbnail_data = thumbnail_data;
}
}
}
then what you need to do is to check if your pojo has value such as error,if error is not null ,you need to handle it .
The fastjson library has help you to avoid crash while converting.
you can find fastjson here

Use this code:
onResponse(JSONObject response){
if(response.getString("code").equalIgnoreCase("success")){
try{
JSONArray array = response.getJsonArray("data");
//do stuff with array
}catch(JSONException e){
JSONObject jsonObject = response.getJsonObject("data");
//do stuff with object
}
}else{
//show your error here
}
}

Related

How to set JSON Array values to seter class in Android Studio with volley

I'm trying to display my row tables on the database in my ListView , However I dont know how to convert JSONObject to JSONArray and set it to my setter class. I get my JSON through Volley String Request
I have the JSON output like this :
{
"error": false,
"message": "Status Fetched",
"status": [
{
"OrderCode": "5x2azu",
"GuestName": "Try",
"ProductName": "Mie Ayam Super Jumbo Komplit",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "58000"
},
{
"OrderCode": "etent3",
"GuestName": "Try",
"ProductName": "Nasi Soto Daging Sapi",
"ProductType": "Soto",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "27000"
},
{
"OrderCode": "ro1eyx",
"GuestName": "Try",
"ProductName": "Mie Ayam Original",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "23000"
}
]
}
and here is my String Request :
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONObject statusJson = obj.getJSONObject("status");
Status status = new Status(
statusJson.getString("OrderCode"),
statusJson.getString("GuestName"),
statusJson.getString("ProductName"),
statusJson.getString("ProductType"),
statusJson.getString("NoTable"),
statusJson.getString("Status"),
statusJson.getString("TotalPrice")
);
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
I dont know how to convert it from JSONObject to JSONArray
here is my Setter and getter class
package com.example.pesanpalgading20.Getter.Status;
import java.util.ArrayList;
public class Status {
private String OrderCode,Name,FoodName,TypeFood,NoTable,Status,TotalPrice;
public Status(){
}
public Status(String orderCode, String name, String foodName, String typeFood, String noTable, String status, String totalPrice) {
OrderCode = orderCode;
Name = name;
FoodName = foodName;
TypeFood = typeFood;
NoTable = noTable;
Status = status;
TotalPrice = totalPrice;
}
public String getOrderCode() {
return OrderCode;
}
public void setOrderCode(String orderCode) {
OrderCode = orderCode;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFoodName() {
return FoodName;
}
public void setFoodName(String foodName) {
FoodName = foodName;
}
public String getTypeFood() {
return TypeFood;
}
public void setTypeFood(String typeFood) {
TypeFood = typeFood;
}
public String getNoTable() {
return NoTable;
}
public void setNoTable(String noTable) {
NoTable = noTable;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getTotalPrice() {
return TotalPrice;
}
public void setTotalPrice(String totalPrice) {
TotalPrice = totalPrice;
}
}
so how do I set the value of the each JSON to SetterandGetter class ?
try {
JSONObject obj = new JSONObject(response);
//if no error in response
if (!obj.getBoolean("error")) {
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
// Add Status In your Array list and enjoy
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
You can use Gson library and convert your json to a model class
To do it first you have to add gson as a dependancy
implementation "com.google.code.gson:gson:2.8.6"
And then since your status in json is array use it like this
JSONArray status = obj.getJSONArray("status");
//Code to convert json array to arraylist of object
Arraylist<Status> arraylist = Gson().fromJson(
status.toString(),
new TypeToken<ArrayList<Status>>(){}.getType()
)
Above code will store your json array status to arraylist variable
so the gettin JSON array is completed but it still display one list from the array this is the code
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
ArrayList<Status> statusList = new ArrayList<Status>();
statusList.add(status);
statusAdapter = new StatusAdapter(getActivity(),statusList);
StatusListView.setAdapter(statusAdapter);
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
I dont know where to put the increment in the setting adapter in
statusList.add(status);
edit : It solved! Thanks to #Pratik Fagadiya
I put the setting adapter outside of the loop and it worked and added more than 1 list

How i can can parse JSON Object ? just a simple object I am new [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
I want to get string values on this one
{"response":"success","Message":"Product Created Successfully"}
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject JO = response.getJSONObject();
String respond = JO.getString("response");
String message = JO.getString("Message");
Toast.makeText(MainActivity.this, respon + message,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
you must write this code in your try{...} block
String respond = response.getString("response");
String message = response.getString("Message");
Toast.makeText(MainActivity.this, respond + message,Toast.LENGTH_SHORT).show();
As a new programmer I recommend you:
A. Read some how you work with Jason.
http://www.vogella.com/tutorials/AndroidJSON/article.html
B. Use the Jason library to make it easy for you down the road.
I'll give you an example of the right use for your needs now.
Build a class that fits your json:
public class MyObject {
private String response;
private String Message;
public MyObject() {
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
}
Add Gson library to your project:
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
Sync your project and now you can cast your json easily to class object:
String json = "{response:success,Message\":Product Created Successfully}";
MyObject myObject = new Gson().fromJson(json, MyObject.class);
String a = myObject.getResponse();
String b = myObject.getMessage();

Error handling using volley in android

I want to print my Error from onErrorResponse using volley in android, i want to print them separately in different textview.
my error from onErrorResponse
{
"message": "422 Unprocessable Entity",
"error": {
"username": [
"The username has already been taken."
],
"email": [
"The email has already been taken."
]
},
"status_code": 422
}
so i want to print them separately,
i mean The username has already been taken. in one textview and The email has already been taken. in 2nd textview. thank
My Code:
public void postData(JSONObject jsonObject) {
String url = "http://www.xxxxxxxx.com/api/v1/auth/register";
String REQUEST_TAG = "volley_key";
JsonObjectRequest jsonObjectReq = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
verifyResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.data != null) {
String errorStr = new String(networkResponse.data);
try {
JSONObject jObj = new JSONObject(errorStr);
JSONObject objError = jObj.getJSONObject("error");
JSONArray emailArray = objError.getJSONArray("email");
if (emailArray != null) {
String emailMessage = String.valueOf(emailArray.get(0));
Toast.makeText(getApplication(), emailMessage, Toast.LENGTH_LONG).show();
}
JSONArray usernameArray = objError.getJSONArray("username");
if (usernameArray != null) {
String usernameMessage = String.valueOf(emailArray.get(0));
Toast.makeText(getApplication(), usernameMessage, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}){
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
}; VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);
}
Add there ErrorModel.class and Error class into java folder.
package co.exmaple;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ErrorModel {
#SerializedName("message")
#Expose
private String message;
#SerializedName("error")
#Expose
private Error error;
#SerializedName("status_code")
#Expose
private Integer statusCode;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
}
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
}
Erro.class
package co.exmaple;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Error {
#SerializedName("name")
#Expose
private List<String> name = null;
#SerializedName("username")
#Expose
private List<String> username = null;
#SerializedName("email")
#Expose
private List<String> email = null;
public List<String> getName() {
return name;
}
public void setName(List<String> name) {
this.name = name;
}
public List<String> getUsername() {
return username;
}
public void setUsername(List<String> username) {
this.username = username;
}
public List<String> getEmail() {
return email;
}
public void setEmail(List<String> email) {
this.email = email;
}
}
Then in StringError Request set Error text into texView.
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
ErrorModel error = gson.fromJson(response,ErrorModel.class);
recyclerView.setAdapter(new ErrorAdapter(ErrorActivity.this,error.getError));
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Make RecyclerView adapter to set Error text to TextView
you can check for the keys inside error object and simply can check if the key exists then show the error for particular Textview.
here is the example.I am taking a case where you are using JsonObject class of volley i.e, the response is in JsonObject.
JsonObject obj=response.getAsJsonObject("error");
if(obj.has("name")){
nameTextView.setText(obj.getAsJsonArray("name").get(0)+"");
}
if(obj.has("username")){
usernameTextView.setText(obj.getAsJsonArray("username").get(0)+"");
}
if(obj.has("email")){
emailTextView.setText(obj.getAsJsonArray("email").get(0)+"");
}

How to post long JSON as a body using retrofit?

I am using retrofit to handle the API calls but facing problem while posting long JSON. I am getting:
internal server error(code : 500)
I need to convert post params to below format.
Body :
{"UserId" : "2",
"PortalId" : "1",
"LocaleId" : "1",
"CatalogId" : "3",
"Items" : [{"Name" : "ap1234","Quantity" : "1"}]}
Below is the code I am using
Api Call :
JSONArray array = new JSONArray();
try {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("Name", "ap1234");
jsonObject1.put("Quantity", "1");
array.put(jsonObject1);
} catch (JSONException e) {
e.printStackTrace();
}
Call data = mApiInterface.getData("application/json","2", "1", "1", "3", array.toString());
addToCart.enqueue(new Callback<DataResponse>() {
Retrofit Interface :
#FormUrlEncoded
#POST(API_ADD_TO_CART)
Call<DataResponse> getData(#Header("Content-Type") String contentType, #Field("UserId") String userId,
#Field("LocaleId") String localeId,
#Field("PortalId") String portalId,
#Field("CatalogId") String CatalogId,
#Field("Items") String Items);
#Body String body has to be used.
JSONArray array = new JSONArray();
try {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("Name", "ap1234");
jsonObject1.put("Quantity", "1");
/*
create a json object pass as body
{"UserId" : "2",
"PortalId" : "1",
"LocaleId" : "1",
"CatalogId" : "3",
"Items" : [{"Name" : "ap1234","Quantity" : "1"}]}
*/
array.put(jsonObject1);
} catch (JSONException e) {
e.printStackTrace();
}
Call data = mApiInterface.getData("application/json","2", "1", "1",
"3", array.toString());
addToCart.enqueue(new Callback<DataResponse>() {
change as following
#FormUrlEncoded
#POST(API_ADD_TO_CART)
Call<DataResponse> getData(#Header("Content-Type") String contentType, #Body String body);
Try using the #Body annotation .
Create a User class add your data to that instance and with retrofit instead of using #field you should send #Body with the User class as body .
example:
interface Foo {
#POST("/jayson")
FooResponse postRawJson(#Body TypedInput body);
}
For more info I found this link to be helpful
https://futurestud.io/tutorials/retrofit-send-objects-in-request-body
retrofit is too typical to implement and this is the easiest method I ever used for retrofit.
public void sendPost(String s1) {
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
apiService.getData(s1).enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
dialog.dismiss();
try {
if (response != null) {
JSONObject jsonObject = new JSONObject(response.body().toString());
String status = jsonObject.optString("status");
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
if (dialog != null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
});
}
I don't think #Header("Content-Type") String contentType is useful in api services, just use #Field to send request
#POST("list_data")
#FormUrlEncoded
Call<JsonObject> shopList(#Field("string") String string);
Thanks for the help guys I did it using #Body
#POST(API_ADD_TO_CART)
Call<ShoppingCartResponse> getData(#Header("Content-Type") String contentType, #Body DataRequest request)
public class AddToCartRequest {
#SerializedName("UserId")
#Expose
private String userId;
#SerializedName("PortalId")
#Expose
private String portalId;
#SerializedName("LocaleId")
#Expose
private String localeId;
#SerializedName("CatalogId")
#Expose
private String catalogId;
#SerializedName("Items")
#Expose
private List<Items> items = null;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPortalId() {
return portalId;
}
public void setPortalId(String portalId) {
this.portalId = portalId;
}
public String getLocaleId() {
return localeId;
}
public void setLocaleId(String localeId) {
this.localeId = localeId;
}
public String getCatalogId() {
return catalogId;
}
public void setCatalogId(String catalogId) {
this.catalogId = catalogId;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

Server responce is in json form

I want to send and receive data from server. For this I've used Volley. The code is in below. Volley can receive the data in Json format. The Server can send and receive data in Json format. How do I convert this Json data into a user readable JAVA format ?? There are about 10 methods in other class. The below class contains the methods for network calls and also interacts with the MainActivity.
public class Api_Volley {
String data;
String flag;
public void my_volley_post (String url , JSONObject jsonObject , final Context context ) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url , jsonObject , new Response.Listener(){
#Override
public void onResponse(Object response) {
String flag = response.toString();
Toast.makeText( context , flag , Toast.LENGTH_LONG ).show();
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context , "Wrong" , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
ApiVolleySingeltonClass.getInstance(context).addToRequestque(jsonObjectRequest);
}
}
Methods in another class:
public void showAllOrderByUserId() {
try {
data_args.put("userId", 2);
} catch (JSONException e) {
e.printStackTrace();
}
try {
data_action.put("action", "showAllOrderByUserId");
data_action.put("args", data_args);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
route = "/order";
new Api_Volley().my_volley_post(addUserUrl + route, data_action, context);
}
Your can use json data like
{"userNodes":[{"id":"1","name":"Enamul Haque"}]}
You can use volley like bellow
private void doLoginAction() {
String url_login = "http://www.lineitopkal.com/android/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url_login,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//pDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray loginNodes = jsonObject.getJSONArray("userNodes");
for (int i = 0; i < loginNodes.length(); i++) {
JSONObject jo = loginNodes.getJSONObject(i);
String id = jo.getString("id");
Log.e("id ::",id);
String name = jo.getString("name");
Log.e("name ::",name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
try {
if (error instanceof TimeoutError ) {
//Time out error
}else if(error instanceof NoConnectionError){
//net work error
} else if (error instanceof AuthFailureError) {
//error
} else if (error instanceof ServerError) {
//Erroor
} else if (error instanceof NetworkError) {
//Error
} else if (error instanceof ParseError) {
//Error
}else{
//Error
}
//End
} catch (Exception e) {
}
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
//Post parameter like bellow
params.put("uname", "era#gmail.com");
params.put("pass", "123456");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
Create your Response format like this
public class Post {
long id;
Date dateCreated;
String title;
String author;
String url;
String body;
}
After making request like below
public void my_volley_post (String url , JSONObject jsonObject , final Context context ) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url , jsonObject , new Response.Listener(){
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
// This will be your Entity
Post post = gson.fromJson(response, Post.class));
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context , "Wrong" , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
ApiVolleySingeltonClass.getInstance(context).addToRequestque(jsonObjectRequest);
}
You have to parse JSON and have to show according to need:
By this you can use GSON (Its easy)
Use this Site to convert JSON to pojo classes.
Click here
These are the References site you can use and implement :
http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html
https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
https://kylewbanks.com/blog/tutorial-parsing-json-on-android-using-gson-and-volley

Categories

Resources