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
Related
So I've implemented google/volley in my apps. When i code the apps i accidentally mistyped the url address and the app just crash suddenly. So how can i avoid this kind of problem. Below are the code i've used.
String url_login = "http://10.0.2.2/test_sstem/public/login";
//Send Post data and retrieve server respond
StringRequest stringRequest = new StringRequest(Request.Method.POST, url_login,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this,"On Response "+response,Toast.LENGTH_LONG).show();
ValidateLogin(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.data != null) {
String jsonError = new String(networkResponse.data);
String message_response=null;
try {
JSONObject object = new JSONObject(jsonError);
message_response= object.getString("error");
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(LoginActivity.this, "On Error " + message_response.toString(), Toast.LENGTH_LONG).show();
showProgress(false);
}
}
})
I know that it can be fixed by correcting the URL, but what if the URL are not alive and working how do we work around this problem.
I have used bellow method for volley which is work for me.. i have used wrong address but my app does not stop. Use bellow full method..
private void doLoginAction() {
pDialog.show();
String url_login = "http://10.0.2.2/test_sstem/public/login";
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("ParentNode");
for (int i = 0; i < loginNodes.length(); i++) {
JSONObject jo = loginNodes.getJSONObject(i);
String key1 = jo.getString("key1");
String key2 = jo.getString("key2");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
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<>();
params.put("uname", "era#gmail.com");
params.put("pass", "123456");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
There can be number of reasons why your app crashes with incorrect url, one could be that the host is un resolvable, you can check the validity of a Url by using the following code:
URLUtil.isValidUrl(url)
This is my Arraylist which I get from the previous fragment,
listoftags = getArguments().getParcelableArrayList("data");
It works well. Now I have to send this with some parameters like below:
public void volleyJsonObjectRequest(final String SessionID , final String CustomerID, final String ServiceState , final String ServiceID, final String Address, final String PaymentMode, final String CustomerComments , final ArrayList Items){
String REQUEST_TAG = "volleyJsonObjectRequest";
// POST parameters
CustomRequest request = new CustomRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Toast.makeText(SignActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
Log.d("response",""+response.toString());
/* String status = response.optString("StatusMessage");
String actionstatus = response.optString("ActionStatus");
Toast.makeText(getActivity(), ""+status, Toast.LENGTH_SHORT).show();
if(actionstatus.equals("Success"))
{
// Intent i = new Intent(SignActivity.this, LoginActivity.class);
// startActivity(i);
// finish();
}*/
dismissProgress();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Error."+error.toString(), Toast.LENGTH_SHORT).show();
Log.d("response",""+error.toString());
dismissProgress();
}
}) {
/* #Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}*/
public String getBodyContentType()
{
return "application/json; charset=utf-8";
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
JSONArray jsArray = new JSONArray(listoftags);
params.put("SessionID", SessionID);
params.put("CustomerID", CustomerID);
params.put("ServiceState", ServiceState);
params.put("ServiceID", ServiceID);
params.put("Address", Address);
params.put("PaymentMode",PaymentMode);
params.put("CustomerComments",CustomerComments);
params.put("Items",jsArray.toString());
return params;
}
};
AppSingleton.getInstance(getActivity().getApplicationContext())
.addToRequestQueue(request, REQUEST_TAG);
}
but it getting error to me I want to send it like
// server side //
{
"SessionID":"9lm5255sg0ti9",
"CustomerID":"9",
"ServiceState":"Karnataka",
"ServiceID":"3",
"Address":"sfaff",
"PaymentMode":"cash",
"CustomerComments":"this is fine",
"Items":[
{
"ItemId":1,
"Cost":6777,
"Quantity":33333
}
]
}
How can send arraylist, with other strings, as raw data using volley on server.
JsonObjectRequest can be used to execute rest api using json as input.
JsonObject jobj = new JsonObject();
jobj.put("key","value");
jobj.put("key","value");
jobj.put("key","value");
jobj.put("key","value");
JsonObjectRequest request = new JsonObjectRequest(requestURL, jobj, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
}
});
*Now add this request in request queue of volley.*
Here jobj is containing input parameters. It can contain even json array inside a JsonObject. Let me know in case of any query.
Rather then volley try retrofit. Make pojo model of your object you want to send, you can make that from pojo classes from https://www.jsonschema2pojo.org the send the whole object on restapi
// try the request //
try {
REQUEST QUEUE
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String URL = url;
JSONObject jsonBody = new JSONObject();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
Iterator itr = listoftags.iterator();
while(itr.hasNext()){
AddRowItem ad=(AddRowItem)itr.next();
jsonObject.put("ItemId:",1);
jsonObject.put("Cost:",ad.getPrices());
jsonObject.put("Quantity:",ad.getQty());
// Log.d("ItemId:",""+1+" "+"Cost:"+ad.getPrices()+" "+"Quantity:"+ad.getQty());
}
jsonArray.put(jsonObject);
JSON VALUES PUT
jsonBody.put("SessionID", "9kp0851kh6mk3");
jsonBody.put("CustomerID", "9");
jsonBody.put("ServiceState", "Karnataka");
jsonBody.put("ServiceID", "3");
jsonBody.put("Address", "Address Demo");
jsonBody.put("PaymentMode", "cost");
jsonBody.put("CustomerComments", "Android Volley Demo");
jsonBody.put("Items", jsonArray);
final String requestBody = jsonBody.toString();
Log.d("string ---- >",""+requestBody);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
showToast("get value"+response.toString());
try {
JSONObject jObj = new JSONObject(response);
String action = jObj.get("ActionStatus").toString();
String status = jObj.getString("StatusMessage");
{"ActionStatus":"Success","StatusMessage":"Order Created","RefIDName":"OrderID","RefIDValue":19}
showToast("get value"+action);
}
catch (JSONException e)
{
showToast("get error"+e.toString());
Log.d("errorissue",""+e.toString());
}
dismissProgress();
}
}, new Response.ErrorListener() {
// error response //
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
showToast("get error"+error.toString());
dismissProgress();
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
dismissProgress();
}
}
}
// THIS IS THE WAY ISSUE RESLOVED //
THANKS EVERYONE ...
im pretty new to Android Studio and I'm trying to build a Get Request using Volley, the Server response is a JsonObject. I tested the code with breakpoints but I wonder why I don't jump into onResponse or why it won't work.
Here's my Code of the Get Method:
public Account GetJsonObject(String urlExtension, String name, Context context) {
String baseURL = "myurl.com/api";
baseURL += urlExtension + "/" + name;
// url will look like this: myurl.com/api/user/"username"
final Account user = new Account("","");
//Account(name, email)
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObject);
return user;
}
As #GVillavani82 commented your onErrorResponse() method body is empty. Try to log the error like this
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
}
Make sure that you have the below permission set in AndroidManifest.xml file and the api URL is proper.
<uses-permission android:name="android.permission.INTERNET"/>
And JsonObjectRequest class returns Asynchronous network call class. Modify your code like below.
// remove Account return type and use void
public void GetJsonObject(String urlExtension, String name, Context context) {
....
.... // other stuffs
....
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
processResponse(response); // call to method
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
});
requestQueue.add(jsonObject);
}
Now create another method like below
private void processResponse(JSONObject response) {
try {
final Account user = new Account("","");
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
} catch (JSONException e) {
e.printStackTrace();
}
}
I am developing an Android app for a Company. While working on the API the company guys gave me a login API something like this
"app.abc.com/Login?data={"email":"abc","pwd":"123"} "
I am using volley library but I don't know how to post data like this since I have not done it before.
I need advice and wanted to know if this is the right way.
Instead of volley ..try to use android networking library
--to do this :add
--compile 'com.amitshekhar.android:android-networking:0.4.0'
to your gradle file.enter code here
AndroidNetworking.post("your login URL")
.setTag("Login")
.addBodyParameter("email", "abc")
.addBodyParameter("pwd", "123")
.setPriority(Priority.IMMEDIATE)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject dataJson = response.getJSONObject("data");
boolean status = dataJson.getBoolean("success");
} else {
JSONArray errorJsonArray = dataJson.getJSONArray("errors");
String errorMsg;
for (int i = 0; i < errorJsonArray.length(); i++) {
errorMsg = errorJsonArray.get(i).toString();
Toast.makeText(LoginActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
}
Log.e("Error", response.toString());
}
} catch (JSONException je) {
je.printStackTrace();
}
}
#Override
public void onError(ANError anError) {
try {
if (anError.getErrorBody() != null) {
JSONObject jsonObject = new JSONObject(anError.getErrorBody());
JSONObject dataJsonObject = jsonObject.getJSONObject("data");
Toast.makeText(getApplicationContext(), dataJsonObject.getString("message"), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), anError.getErrorDetail(), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
Log.e("Error", anError.getErrorDetail());
}
});
Using Volley - Create the request queue
RequestQueue queue = Volley.newRequestQueue(this); // this = context
Then create the postRequest, should look somthing like (using Volley):
url = "app.abc.com/Login";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("data", "{\"email\":\"abc\",\"pwd\":\"123\"}");
return params;
}
};
queue.add(postRequest);
I also recommend using Retrofit and GSON libraries (a bit learning curve here but it is worth it since they are very powerful libraries)
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
}
}