How i can retrieve this array of images from json? - android

I have his json and i need to retrieve array of images from images Variable..
this my json :
{
id: 215,
title: " this is title",
image: "1464958558.jpg",
description: "description",
images: "["1464958558.jpg","1464958559.jpg","1464958561.jpg","1464958563.jpg","1464958564.jpg","1464958568.jpg","1464958569.jpg","1464958570.jpg","1464958573.jpg","1464958574.jpg"]",
user_name: "user name",
telephone: "0123456789"
}
i want to retrieve this images array and display it in grid view... and i can't find any response in logcat
my doInBackGround()
#Override
protected Integer doInBackground(String... arg0) {
// Building Parameters
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
ID = mCursor.getString(ArticleLoader.Query._ID);
String jsonStr = sh.makeServiceCall(String.valueOf(Config.BASE_URL));
//jsonStr = sh.makeServiceCall(url + 373); //or url + ID
Log.i(TAG, "doInBackground Image: " + String.valueOf(Config.BASE_URL));
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
GridItem item;
item = new GridItem();
Log.i(TAG, "parseResult: " + jsonObj);
for (int i = 0; i < jsonObj.length(); i++) {
// if (response != null) {
String images = jsonObj.getString("images");
item.setImage(IMAGE_LINK + images);//IMAGE_LINK +
Log.i(TAG, "parseResult: " + IMAGE_LINK + " " + images);
mGridData.add(item);
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return null;
}
dose i retrieve data correctly ?

Is images json object string or json array? In your code is string.
If WebService changes are possible, request to given you a json array of images, this is best practice.
If not possible you must parse json image object with regex.

No your doing wrong just try below code
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray arrayObj = jsonObj.getJSONArray("images");
for(int i=0;i<arrayObj.length();i++)
{
String item = arrayObj.getString(i);
// now you use item string where you want
}
} catch (JSONException e) {
e.printStackTrace();
}

images should be a JSONArray, and hence you can do that to parse it (inside your for loop!):
JSONArray imagesJSON = jsonObj.getJSONArray("images");
for(int k = 0 ; k<imagesJSON.length() ; k++){
String image = imagesJSON.getString(k);
item.setImage(IMAGE_LINK + image);//IMAGE_LINK +
Log.i(TAG, "parseResult: " + IMAGE_LINK + " " + image);
mGridData.add(item);
}

Related

Convert Volley ArrayList Response data to String

I am using the andriod volley library to get some array of data i want to convert the data into a string but when i tried doing this am only able to get the last data in the array when they are up to ten names only the last name is been converted to string.Here is the code i am using i want to get all the name in as string separated by comma
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
JSONArray jsonArray = response.getJSONArray("users");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
ArrayList<String> artistClassArrayList3 = new ArrayList<String>();
artistClassArrayList3.add(name);
StringBuilder sbString = new StringBuilder("");
for(String language : artistClassArrayList3){
sbString.append(language).append(", ");
}
String strList = sbString.toString();
if( strList.length() > 0 )
strList = strList.substring(0, strList.length() - 1);
textView.setText(strList);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error J :: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}, error -> Toast.makeText(getApplicationContext(), "Error R:: " + error.getMessage(), Toast.LENGTH_SHORT).show());

How to retrieve genre from JSON result

I can not retrieve all key values from JSON object retrieved from HTTP request.
This is the returned JSON object from http request:
{"data":[{"movie_id":3,"movie_name":"Promithius","genre":"a dude","year":"2016","rating":45}]}
my Android Code:
try {
//HttpJsonParser httpJsonParser = new HttpJsonParser();
JSONObject JSonObj = (JSONObject) new
JSONTokener(result).nextValue();
String mymovie = JSonObj.getString("movie_name" );
String movieGenre = JSonObj.getString("genre" );
etResponse.setText("movie=" + mymovie + " genre=" +
movieGenre);
} catch (JSONException e) {
etResponse.setText("ERROR=" + e.getMessage());
e.printStackTrace();
}
When I run it in emulator with only:
String mymovie = JSonObj.getString("movie_name" );
etResponse.setText("movie=" + mymovie;
I get back the Movie name with no error. So the issue is I can retrieve movie_name but no Genre.
Error returned says "No value for genre"
Thanks in advance.
I think the data is JSONArray.
String result = "{\"data\":[{\"movie_id\":3,\"movie_name\":\"Promithius\",\"genre\":\"a dude\",\"year\":\"2016\",\"rating\":45}]}";
try {
JSONObject JSonObj = (JSONObject) new JSONTokener(result).nextValue();
JSONArray data = JSonObj.getJSONArray("data");
for (int i=0; i<data.length(); i++){
JSONObject jsonObject = data.getJSONObject(i);
String mymovie = jsonObject.getString("movie_name" );
String movieGenre = jsonObject.getString("genre" );
String dest = "movie=" + mymovie + " genre=" + movieGenre;
Log.d(TAG, dest);
}
} catch (JSONException e) {
e.printStackTrace();
}
Below is the code that finally worked for me. I hope it can help someone else.
try{
JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("data");
object = Jarray.getJSONObject(0);
String MovieName = object.getString("movie_name");
String Genre = object.getString("genre");
String Rating = object.getString("rating");
String Year = object.getString("year");
etResponse.setText("Movie Name = " + MovieName + " \n Genre = " +
Genre + "\n Rating = " + Rating + " \n Year = " + Year );
} catch (JSONException e) {
etResponse.setText("JSONException: " + e.getMessage());
}

How to print image that is in byte array using json

i am developing an app,in that i am calling services using json. In that whatever string data is there i am displaying it using textView. But i want to display image that is in byte array.Below is my code:
t1 = (TextView) findViewById(R.id.t1);
i = (ImageView) findViewById(R.id.imageview);
String url = "url"
AQuery mAQuery = new AQuery(MainActivity.this);
mAQuery.ajax(url, String.class, new AjaxCallback<String>() {
#Override
public void callback(String url, String data, AjaxStatus status) {
super.callback(url, data, status);
if (android.support.v7.appcompat.BuildConfig.DEBUG) {
Log.d("###$Request URL", url + "");
Log.d("###$Response ", data + "");
Log.d("###$Status Message : ", status.getMessage() + "");
Log.d("###$Status Code : ", status.getCode() + "");
}
if (status.getCode() != -101) {
String StringData = "" + data;
try {
JSONObject json = new JSONObject(StringData);
int sd;
sd = json.getInt("Stu_id");
String Name = json.getString("Stu_FName");
String LastName = json.getString("Stu_LName");
String Degree = json.getString("DegreeName");
t1.setText("Details OF STUDENTS");
t1.setText( "First Name: " + Name + "\n" + "Last Name:" + LastName + "\n" + "Degree: " + Degree);
} catch (JSONException e) {
}
} else {
}
}
});
}
}
I am not getting how to do that.I am getting data from server like this:
{"Comp_Name":null,"DegreeName":"B.E\/B.Tech","EVENT_DATE":null,"Event_LOC":null,"Event_Time":null,"Event_Title":null,"Event_id":null,"HET_UID":null,
"Image":[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,225,0,42,69,120,105,102,0,0,73,73,42,0,8,0,0,0,1,0,49,1,2,0,7,0,0,0,26,0,0,0,0,0,0,0,71,111,111,103,108,101,0,0,255,219,0,132,0,3,2,2,3,8,11,11,9,13,11,11,10,10,11,10,11,13,11,10,10,10,13,13,11,10,11,11,10,8,8,14,13,14,8,10,8,13,10,9,10,11,15,10,13,10,14,10,10,8,10,10,10,10,8,10,11,15,10,10,13,8,10,9,8,1,3,4,4,6,5,6,10,6,6,10,16,13,11,13,13,13,16,16,13,16,16,15,16,15,15,15,13,15,15,13,13,13,13,14,15,15,13,14,13,13,15,15,13,15,13,15,13,15,15,13,13,13,13,13,14,13,13,13,13,15,13,13,13,13,255,192,0,17,8,0,88,0,88,3,1,17,0,2,17,1,3,17,1,255,196,0,29,0,0,2,3,1,1,1,1,1,0,0,0,0,0,0,0,0,6,8,4,5,7,9,3,1,2,0,255,196,0,62,16,0,2,1,1,7,1,5,6,3,5,7,5,0,0,0,0,1,2,3,17,0,4,5,6,18,33,49,65,7,19,34,81,97,8,50,66,113,129,145,161,177,240,35,98,146,193,209,20,51,82,114,178,179,225,9,21,22,115,147,255,196,0,28,1,0,2,2,3,1,1,0,0,0,0,0,0,0,0,0,0,4,5,3,6,0,1,2,7,8,255,196,0,54,17,0,1,4,0,4,3,5,7,4,1,5,1,0,0,0,0,1,0,2,3,17,4,18,33,49,5,65,81,19,34,97,113,145,20,129,161,177,209,225,240,6,21,35,50,241,51,82,114,130,193,66,255,218,0,12,3,1,0,2,17,3,17,0,63,0,125,38,184,169,232,45,26,29,76,195,114,196,78,53,26,40,233,225,220,250,250,15,206,219,13,180,124,88,108,192,23,29,249,42,140,77,110,49,50,179,78,34,5,252,20,66,89,202,138,178,210,154,87,110,164,249,157,38,134,194,98,49,17,97,168,204,224,208,77,2,121,157,233,54,195,112,217,68,132,177,185,128,26,251,244,215,236,161,227,185,167,49,68,242,77,174,56,97,86,69,137,25,12,179,204,24,70,0,238,132,145,4,37,155,81,0,187,183,3,186,165,28,129,22,103,18,70,131,119,19,77,104,26,222,196,155,255,0,168,27,119,183,82,195,133,100,237,22,28,44,18,110,154,5,95,252,189,219,120,217,218,171,179,102,130,251,120,145,131,68,232,34,9,121,210,84,204,47,40,236,225,89,96,146,75,188,65,68,167,195,173,156,41,140,22,114,11,133,88,89,79,110,230,66,224,98,0,157,141,217,59,130,107,187,167,77,119,234,139,196,181,177,97,114,58,201,46,208,26,203,150,171,157,59,54,158,183,182,139,87,143,40,225,139,196,117,52,216,177,39,127,150,170,126,86,113,100,164,177,98,31,22,145,211,71,128,3,227,86,179,11,146,192,111,18,221,123,163,28,179,3,36,241,67,124,33,154,137,28,12,233,173,209,225,66,4,98,177,0,132,233,30,25,25,157,171,184,166,99,37,196,197,35,29,144,49,164,16,208,199,102,22,72,190,209,164,10,36,155,167,19,117,165,90,117,63,11,195,140,27,241,15,121,34,71,130,92,67,199,122,128,2,216,78,165,172,162,219,110,196,235,204,18,247,136,92,176,217,157,159,254,225,18,184,110,236,72,75,133,4,130,40,237,226,49,163,135,1,123,201,8,73,181,3,18,105,140,43,196,241,247,96,164,3,26,215,237,89,178,176,7,1,125,226,27,149,165,219,52,229,3,64,28,64,178,77,131,129,240,30,227,134,16,195,32,177,96,189,238,118,182,72,57,179,16,104,146,51,115,182,139,173,8,239,243,94,11,129,46,236,64,163,48,220,131,184,220,239,215,174,255,0,106,90,237,27,238,188,82,190,55,195,97,151,14,113,56,96,45,187,215,78,123,105,162,254,155,38,97,50,114,138,126,96,88,149,230,118,169,239,125,142,101,199,248,0,255,0,46,223,149,165,108,142,105,208,161,164,130,57,7,125,160,251,146,135,15,183,15,104,177,252,40,127,204,15,242,54,173,137,159,213,95,93,194,7,34,154,220,135,237,19,112,191,65,27,137,40,52,141,77,175,68,138,245,163,6,143,91,42,141,189,211,95,3,2,173,80,13,154,197,32,34,211,204,14,21,173,254,218,157,168,128,71,215,203,95,59,10,94,107,237,13,229,129,30,37,149,163,239,8,107,204,108,138,170,213,11,65,52,208,74,52,147,77,93,218,238,84,130,219,20,32,226,113,50,199,35,76,113,135,142,164,88,7,200,17,183,95,130,46,88,131,101,160,70,215,151,93,119,220,10,178,57,95,95,122,49,195,251,56,184,202,133,221,158,105,24,105,145,226,114,27,79,4,11,211,168,145,128,0,10,69,29,217,88,26,247,64,150,39,169,161,118,43,253,119,23,15,246,139,107,7,147,65,215,204,146,82,63,105,153,206,170,12,28,172,115,229,221,26,15,51,117,213,31,118,99,145,176,43,148,101,110,224,164,85,58,99,162,133,74,248,142,202,160,179,51,22,119,146,82,242,200,236,204,206,196,146,103,138,38,198,41,191,149,160,244,217,46,198,205,43,233,179,52,2,57,139,215,199,115,240,209,24,200,205,105,169,46,10,131,53,229,248,47,40,81,135,60,50,154,50,215,99,70,216,169,245,86,7,212,91,116,10,97,133,156,192,236,205,244,59,31,49,177,64,87,78,199,238,71,64,145,230,147,186,98,99,47,51,181,42,120,101,145,153,100,29,55,21,166,221,44,190,78,27,4,165,174,149,185,178,155,25,181,163,212,3,98,254,92,147,195,198,36,140,59,177,13,102,112,3,178,180,54,235,197,160,17,245,69,88,206,3,117,157,116,183,52,217,186,139,51,164,150,44,67,226,39,46,199,113,200,143,21,139,99,121,198,249,115,145,161,120,201,35,134,28,48,232,71,235,157,186,90,9,49,25,13,16,129,111,7,124,182,248,142,159,155,168,75,218,168,63,1,22,227,219,27,209,103,236,56,143,5,199,69,237,115,26,244,251,90,15,103,9,135,183,72,182,191,99,220,103,48,98,120,180,23,93,187,183,73,251,230,166,233,15,114,234,204,61,65,101,0,117,102,2,219,16,128,84,209,99,94,93,103,146,236,54,1,128,97,144,196,145,172,106,18,33,68,94,137,79,83,241,121,177,220,146,77,106,77,75,21,201,106,222,247,18,78,167,212,169,23,108,107,13,135,96,241,165,106,118,53,60,249,150,59,219,17,231,9,44,187,177,199,243,166,139,222,60,227,117,233,40,63,81,248,87,159,235,108,181,195,184,115,255,0,250,140,171,75,166,100,45,189,65,31,175,47,233,231,229,109,165,242,96,192,52,1,10,90,227,112,183,167,175,75,96,80,28,51,155,182,170,53,246,91,72,177,129,81,75,138,208,219,97,76,99,181,149,123,68,93,175,15,115,150,120,191,191,129,25,210,163,102,10,53,21,250,129,183,145,3,157,237,4,177,135,181,17,134,149,208,59,78,107,155,18,251,107,227,171,177,137,106,54,178,206,196,163,63,116,120,228,150,9,178,206,32,187,21,63,107,20,217,218,118,41,19,160,123,119,9,222,255,0,166,38,14,35,151,16,188,178,144,201,20,40,28,142,21,154,86,42,58,212,148,83,205,60,35,214,210,7,89,69,224,225,50,60,50,183,41,231,189,94,241,219,199,26,130,143,119,225,90,115,239,22,21,62,160,31,91,111,85,232,145,123,30,12,81,35,53,107,204,221,116,214,149,93,227,44,223,69,89,165,74,249,23,224,252,199,30,86,213,34,135,24,195,208,107,90,125,2,243,139,4,189,173,0,42,107,77,245,249,250,158,105,233,95,151,91,106,138,235,247,124,59,181,117,143,10,250,41,168,216,164,102,160,56,3,114,71,20,243,168,231,245,197,186,4,174,196,152,89,244,204,211,124,143,208,169,183,30,209,177,5,59,157,107,78,167,125,205,57,231,253,92,125,250,14,33,65,55,6,133,205,238,247,79,135,211,252,34,156,23,180,59,180,191,34,72,0,237,198,196,131,212,215,106,122,113,210,210,7,90,170,227,56,84,144,119,143,168,248,88,228,189,175,247,255,0,225,60,31,95,34,63,29,185,22,148,20,163,33,247,160,236,123,23,93,50,33,255,0,9,251,80,244,245,183,46,43,135,55,75,92,95,206,55,123,176,189,76,171,178,9,158,149,230,154,141,129,40,70,11,125,38,242,249,151,240,54,39,84,96,31,149,169,145,226,28,213,244,22,39,244,108,50,183,248,220,153,31,102,140,159,134,193,118,98,160,5,146,70,145,133,57,210,59,180,83,230,42,24,252,182,234,109,105,193,59,60,121,143,53,229,220,71,5,251,102,37,208,180,217,20,47,204,89,62,132,15,138,14,246,141,246,141,198,46,228,67,117,86,123,196,149,1,62,21,10,40,93,141,106,160,84,13,32,84,182,154,53,42,203,60,211,6,110,105,5,144,68,192,72,179,203,239,249,170,88,223,48,118,130,230,179,94,111,8,127,194,140,85,7,7,221,7,211,173,108,161,248,155,254,165,66,37,121,58,232,180,124,183,142,231,232,128,116,190,202,104,43,251,69,87,31,93,75,170,159,38,4,83,98,45,19,49,206,5,73,103,158,170,235,47,123,90,231,75,187,232,146,15,237,27,208,180,39,75,157,205,105,28,135,73,39,255,0,106,210,159,102,76,198,10,213,70,94,47,188,223,69,184,101,111,104,156,167,125,211,12,138,97,157,248,73,129,141,245,26,108,9,33,100,210,41,82,140,235,206,246,53,147,49,250,2,141,135,17,44,127,233,187,78,135,84,101,62,7,3,209,225,112,232,131,81,4,248,141,1,167,136,13,252,71,86,196,112,118,230,211,121,39,240,113,102,63,187,59,104,157,47,150,190,28,180,235,126,106,70,87,207,26,199,119,38,218,170,0,234,64,39,226,247,117,108,72,35,98,41,201,38,189,130,150,113,46,29,217,255,0,44,91,85,159,15,183,201,83,118,165,124,238,99,50,30,2,154,159,49,165,141,121,234,57,29,8,54,199,157,21,92,157,215,33,214,225,120,188,206,66,10,151,144,211,234,198,193,189,192,13,82,230,135,57,221,208,159,252,103,30,201,206,229,92,17,67,200,181,80,152,28,121,175,125,195,193,198,162,132,58,39,3,99,98,181,179,143,225,87,28,59,188,5,82,34,11,2,221,20,84,214,157,121,39,235,107,68,25,89,16,201,178,243,44,83,230,151,22,227,136,61,241,191,159,229,4,164,226,120,182,123,188,177,189,67,118,119,105,79,188,74,41,142,61,244,40,51,54,251,29,76,85,37,33,217,168,173,176,8,101,146,57,164,239,187,65,231,255,0,159,100,20,206,121,213,161,84,102,27,230,126,71,88,158,11,197,227,197,24,212,151,118,49,132,117,44,236,111,29,218,4,104,118,26,76,106,172,117,26,138,5,98,29,6,31,179,46,15,0,244,252,41,59,102,156,73,69,186,125,233,48,25,59,43,222,5,215,89,27,232,167,215,139,87,195,133,23,114,79,11,13,128,178,235,254,90,205,8,210,20,143,79,119,27,73,222,17,64,204,16,184,72,217,161,144,75,33,247,116,170,108,199,251,192,70,146,219,11,27,37,213,206,3,222,130,196,189,241,255,0,80,79,186,253,124,16,100,89,223,51,94,155,185,158,233,121,104,77,0,146,75,156,131,125,32,146,28,93,163,101,0,236,11,192,181,166,173,107,93,34,105,160,100,125,232,222,47,207,243,230,161,195,205,35,199,125,191,10,250,162,172,149,218,174,107,193,136,90,188,215,34,192,176,230,88,199,21,86,98,53,170,244,82,192,138,146,9,217,108,70,31,27,179,92,143,178,52,58,132,209,221,179,78,31,136,194,47,144,82,161,6,133,31,24,160,220,121,211,130,126,50,73,229,72,47,90,236,194,194,123,128,198,87,240,74,108,59,64,122,120,31,15,146,15,237,223,53,223,103,194,47,154,60,114,67,17,174,196,120,106,171,33,167,238,171,51,210,191,9,226,182,195,168,73,56,174,19,176,121,45,26,27,175,162,69,59,43,185,160,101,110,14,161,249,217,92,166,208,156,52,134,189,174,61,83,35,136,118,125,138,62,226,132,250,154,126,118,15,246,57,152,52,32,175,96,195,126,177,136,10,115,74,190,237,58,245,43,67,135,220,27,221,209,23,122,163,169,21,148,169,232,65,211,164,154,238,9,243,180,248,140,208,97,203,78,224,125,151,157,98,167,110,35,17,52,205,217,239,53,228,181,108,15,43,101,233,99,2,77,246,30,235,21,167,213,88,31,202,212,200,156,208,123,197,68,224,224,52,66,55,243,147,46,178,50,35,0,87,118,121,165,102,234,60,43,222,57,223,209,125,43,210,211,56,185,219,108,182,14,154,162,143,252,223,46,58,133,142,90,185,248,90,129,127,138,180,31,80,45,163,24,13,231,229,201,104,56,222,180,162,97,184,102,92,191,147,31,120,209,78,188,52,114,21,212,62,96,209,135,163,43,0,120,230,182,150,49,148,105,241,217,105,238,35,113,97,94,199,217,117,210,236,132,153,166,149,186,9,100,5,71,200,42,129,248,125,109,220,151,150,202,128,59,49,208,44,175,180,91,189,219,75,84,1,96,227,39,50,149,192,5,144,246,41,218,116,184,109,245,174,203,78,238,74,201,24,99,69,71,173,31,150,11,189,67,110,9,169,98,5,105,75,174,14,67,146,202,93,218,12,197,157,121,173,211,180,28,98,231,20,83,184,58,161,154,239,50,238,104,26,177,185,147,110,116,134,96,6,222,21,32,114,13,153,23,116,79,36,153,184,140,25,15,254,205,249,13,143,191,159,138,67,50,216,147,80,241,233,21,243,178,249,124,21,41,143,35,64,87,93,229,236,171,12,238,205,87,197,78,108,245,211,158,210,129,210,209,80,226,28,214,7,29,233,41,94,208,56,67,221,37,133,212,146,2,120,73,243,5,197,62,149,178,238,49,1,105,32,141,192,40,188,38,49,152,134,23,198,118,114,171,203,221,169,57,143,114,105,75,80,157,7,121,56,18,2,16,222,100,204,249,98,241,119,253,171,0,198,66,192,114,196,240,40,6,251,10,47,151,222,204,34,99,218,234,104,67,151,52,238,80,125,202,229,144,92,133,121,216,10,10,107,73,60,39,215,160,249,138,253,41,99,234,81,201,69,76,228,86,174,248,254,91,185,37,220,194,224,232,106,106,86,174,165,126,73,115,187,26,128,106,78,192,83,110,2,217,26,95,98,148,192,214,232,131,27,237,196,233,166,170,129,96,123,39,187,66,137,5,163,85,159,99,249,244,222,20,215,233,243,180,236,134,138,18,105,5,37,227,56,99,111,21,238,25,1,217,15,136,244,1,136,27,147,176,234,126,66,214,140,43,123,133,87,222,243,156,20,195,246,155,154,37,124,51,115,226,212,168,160,108,0,101,53,80,167,99,183,136,208,10,13,59,111,82,72,114,38,89,203,90,225,212,37,50,250,36,94,182,232,81,73,237,119,156,93,67,252,107,191,74,143,235,105,181,232,16,126,210,224,235,89,71,180,7,98,87,171,253,198,88,227,72,222,116,163,192,73,163,6,14,172,202,172,58,200,129,144,43,120,11,20,38,154,67,164,120,201,100,145,142,47,26,80,248,115,69,96,167,142,46,238,218,159,143,95,34,145,44,149,135,220,37,170,183,206,156,31,183,242,54,167,203,96,171,123,13,132,27,158,125,159,177,6,145,29,29,152,49,212,177,215,112,117,61,52,29,137,127,37,169,106,241,189,5,156,225,49,34,170,181,81,140,31,106,78,186,14,75,194,111,103,60,96,43,55,123,60,116,93,215,198,104,20,212,6,33,180,168,21,170,212,211,81,216,123,196,49,18,245,165,203,176,12,53,151,243,201,125,201,189,139,99,49,178,153,36,99,9,126,14,236,54,39,81,106,144,43,182,213,111,82,14,214,89,139,157,149,221,26,174,91,135,115,29,68,162,236,195,151,110,168,116,3,81,231,94,150,86,199,19,170,45,218,44,235,180,76,225,5,219,76,96,238,121,244,81,215,111,195,169,161,242,179,44,54,28,201,101,38,197,226,3,69,47,78,198,243,29,196,36,237,120,141,206,169,53,68,92,120,74,211,247,142,161,66,120,17,0,65,30,38,169,210,199,17,129,149,249,123,58,20,40,242,65,97,177,1,129,217,129,54,87,166,97,199,46,243,133,12,211,50,45,116,199,28,44,20,117,217,168,212,27,208,17,25,28,245,173,142,102,13,224,81,35,231,244,65,62,73,30,73,160,61,77,124,7,205,6,205,134,225,46,64,238,239,132,249,119,71,115,255,0,194,131,206,161,206,199,109,197,45,59,112,213,205,115,103,243,252,166,24,246,217,140,251,253,252,163,203,115,75,55,119,233,172,69,210,86,49,176,29,85,141,195,218,131,63,160,33,47,26,143,77,91,216,103,254,155,196,85,19,167,154,223,180,179,118,238,178,241,158,177,30,253,239,18,42,6,146,66,238,34,217,106,199,83,16,191,14,163,86,32,108,9,52,160,216,36,199,240,25,224,102,96,210,90,6,251,215,137,240,241,245,86,12,7,18,14,166,60,140,223,63,186,218,241,59,165,242,253,119,73,97,33,128,28,87,229,183,31,162,45,83,143,248,205,57,89,163,148,131,153,168,125,50,150,116,102,82,209,191,134,148,44,73,3,166,213,216,88,147,48,173,209,62,209,33,25,85,231,105,25,191,13,184,192,17,221,67,129,189,88,123,223,63,215,79,75,15,20,18,98,31,76,9,124,147,8,197,185,47,39,180,75,148,190,62,249,69,122,80,150,30,132,26,21,250,138,31,91,89,227,225,109,3,188,239,68,161,248,226,237,130,165,75,214,84,142,78,250,75,193,103,45,195,141,75,199,135,246,74,164,81,121,12,106,84,238,8,230,205,227,130,54,10,9,107,221,156,230,114,58,186,255,0,102,101,12,146,43,43,13,152,110,167,212,26,26,126,129,7,113,99,52,228,178,215,238,247,60,228,83,79,213,24,110,124,192,215,171,214,148,36,113,191,39,10,218,162,196,47,216,208,33,85,123,192,42,75,144,192,183,146,131,213,189,116,211,106,19,231,206,171,73,233,188,100,124,155,39,135,187,26,22,188,39,95,181,172,133,211,183,115,175,154,171,181,241,60,119,117,10,147,28,200,185,58,40,201,72,134,177,251,134,191,149,182,195,51,157,78,118,158,106,66,230,180,88,9,99,237,191,42,92,227,159,246,35,194,84,18,41,193,181,195,135,67,218,68,76,137,94,42,82,215,14,207,162,8,203,189,168,227,120,107,21,71,233,86,66,118,223,211,161,255,0,131,111,39,253,85,195,48,145,226,0,128,83,143,246,3,96,119,20,57,19,185,27,109,160,87,94,11,142,157,209,127,46,189,58,248,218,186,198,61,177,243,131,47,116,160,234,60,116,3,241,59,90,161,23,13,205,185,209,60,126,59,46,195,85,146,99,184,230,59,121,146,178,43,78,238,104,80,171,48,61,118,8,106,41,230,173,176,169,36,212,214,201,12,13,136,101,96,74,100,145,210,27,114,33,195,123,45,186,48,6,72,163,135,110,3,72,207,254,249,69,63,199,229,75,22,26,20,89,2,149,136,246,11,130,83,103,146,191,186,224,144,104,15,186,195,126,126,20,167,77,85,4,91,179,24,11,121,2,163,193,178,230,99,185,63,119,11,172,145,187,80,172,149,26,77,11,86,160,26,86,148,168,21,169,2,134,209,128,66,231,41,10,219,26,205,19,192,66,202,134,38,60,26,234,141,254,78,63,38,1,186,233,28,219,45,116,77,110,190,93,187,64,41,77,245,163,108,175,93,193,242,39,169,29,9,228,122,131,93,230,91,181,255,217],"Position_Name":null,"Spec_name":"Computer Science","State_Name":null,"Stu_FName":"sdsff","Stu_LName":"fgfgfgg"
Parse your byte array Image from your json to a byte array variable.
Convert your byte array to bitmap like below
Bitmap bmap = BitmapFactory.decodeByteArray(image, 0, image.length); // image is your byte array
Set the bitmap bmap to an ImageView
imageView.setImageBitmap(bmap);
Update
Try this:
a. Get your byte array like this
JsonArray arr = json.getJSONArray("Image");
b. Create byte array object with size of your Json array arr
byte[] imageArr = new byte[arr.length()];
c. Iterate to insert elements into your byte array
for(int i = 0; i < arr.length(); i++){
imageArr[i] = arr.getInt(i);
}
Then follow the steps 1 and 2 to achieve your goal.

Android: JSONException: Value null at VesselList of type org.json.JSONObject$1 cannot be converted to JSONArray

I'trying to Parsing JSOn object but sometime it runs properly sometime getting followoing error:
org.json.JSONException: Value null at VesselList of type
org.json.JSONObject$1 cannot be converted to JSONArray
public void getCompanyDeatails()
{
String strUrl_companyDeatls = "http://103.24.4.60/CLASSNK1/MobileService.svc/Get_Company/Sync_Time/"+strSync_Time+"/Authentication_Token/"+str_Authentication_Token;
Log.e("strUrl_companyDeatls ", " = " + strUrl_companyDeatls);
InputStream inputStream = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(strUrl_companyDeatls));
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
strResult = convertInputStreamToString(inputStream);
else
strResult = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
String jsonStr = strResult;
Log.e("jsonStr ", " = " + jsonStr);
if (jsonStr != null)
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String jsonResult = jsonObj.toString().trim();
Log.e("jsonResult ", " = " + jsonResult);
JSONObject companyList = jsonObj.getJSONObject("Get_CompanyResult");
Log.e("companyList ", " = " + companyList.toString());
JSONArray jarr = jsonObj.getJSONArray("CompanylList");
Log.e("jarr ", " = " + jarr.toString());
for (int i = 0; i < jarr.length(); i++) {
JSONObject jobCompanyDetails = jarr.getJSONObject(i);
str_CompanyId = jobCompanyDetails.getString("Company_ID");
str_CompanyName = jobCompanyDetails.getString("Company_Name");
Log.e("str_CompanyId ", " = " + str_CompanyId);
Log.e("str_CompanyName ", " = " + str_CompanyName);
if (dbhelper.isTitleExist(str_CompanyId)) {
//Upadte
dbhelper.updatedetails(str_CompanyId, str_CompanyName);
Log.e("Data updated in ", "Company Table !!");
} else {
//insert
dbhelper.insertCompany(str_CompanyId, str_CompanyName);
Log.e("Data inserted in ", "Company Table !!");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
this is my JSON String
{"Get_CompanyResult":{"CompanylList":[{"Company_ID":93,"Company_Name":"SeaChange"},{"Company_ID":97,"Company_Name":"VM 2"}],"Sync_Time":"2015-09-11 12:44:17.533"}}
Is this code is right?
Here:
JSONArray jarr = jsonObj.getJSONArray("CompanylList");
line causing issue because CompanylList JSONArray is inside Get_CompanyResult JSONObject instead of main which is jsonObj.
Get CompanylList JSONArray from companyList JSONObject:
JSONArray jarr = companyList.getJSONArray("CompanylList");
Change your code according to here :
public void getCompanyDeatails() {
String strUrl_companyDeatls = "http://103.24.4.60/CLASSNK1/MobileService.svc/Get_Company/Sync_Time/" + strSync_Time + "/Authentication_Token/" + str_Authentication_Token;
Log.e("strUrl_companyDeatls ", " = " + strUrl_companyDeatls);
InputStream inputStream = null;
String strResult = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet("strUrl_companyDeatls"));
inputStream = httpResponse.getEntity().getContent();
// strResult = "{\\\"Get_CompanyResult\\\":{\\\"CompanylList\\\":[{\\\"Company_ID\\\":93,\\\"Company_Name\\\":\\\"SeaChange\\\"},{\\\"Company_ID\\\":97,\\\"Company_Name\\\":\\\"VM 2\\\"}],\\\"Sync_Time\\\":\\\"2015-09-11 12:44:17.533\\\"}}";
String jsonStr=null;
if (inputStream != null) {
strResult = convertInputStreamToString(inputStream);
jsonStr = strResult;
}
Log.e("jsonStr ", " = " + jsonStr);
if (jsonStr != null) {
JSONObject jsonObj = new JSONObject(jsonStr);
String jsonResult = jsonObj.toString().trim();
Log.e("jsonResult ", " = " + jsonResult);
JSONObject companyList = jsonObj.getJSONObject("Get_CompanyResult");
Log.e("companyList ", " = " + companyList.toString());
JSONArray jarr = companyList.getJSONArray("CompanylList");
Log.e("jarr ", " = " + jarr.toString()); // error was here. You need to use companyList json object because it contains company list.
for (int i = 0; i < jarr.length(); i++) {
JSONObject jobCompanyDetails = jarr.getJSONObject(i);
String str_CompanyId = jobCompanyDetails.getString("Company_ID");
String str_CompanyName = jobCompanyDetails.getString("Company_Name");
Log.e("str_CompanyId ", " = " + str_CompanyId);
Log.e("str_CompanyName ", " = " + str_CompanyName);
// if (dbhelper.isTitleExist(str_CompanyId)) {
// //Upadte
// dbhelper.updatedetails(str_CompanyId, str_CompanyName);
// Log.e("Data updated in ", "Company Table !!");
// } else {
//
// //insert
// dbhelper.insertCompany(str_CompanyId, str_CompanyName);
// Log.e("Data inserted in ", "Company Table !!");
// }
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
}
Note :
There is no need to use nested try and catch.
For 'strResult = "Did not work!";', always make sure that every
exception handled well (at checkpoint of jsonstr is null or not you
can not identify error occurred at parsing input stream can lead to
serious bugs).
HttpClient is deprecated so make sure you use latest version of any
third party library like Volley or Retrofit for network
operation and GSON for parsing response. It will minimize your
efforts.
Thanks.

How do you pass results from a JSON array into into a multidimensional array?

I am a little new to android/java. I am trying to pass JSON values into a list and then into a multidimensional array. I am not having much success.
2 questions,
1) How would I load all of the variables in a json array into children[][]?
2) How do you view children[][] in Log.i
Herei s my code:
List<String> cList = new ArrayList<String>();
String customer_name, customer_title, customer_postal_code, customer_city, customer_state, customer_street_address;
ArrayList<String> cTitle, cClubName, cPostalCode, cCity, cState, cStreet = new ArrayList<String>();
public String[][] children = null;
//... onCreate method, HTTP Connection, StringBuilder, etc. These work fine...
// Pass data into array
try{
JSONArray jArray = new JSONArray(result);
JSONObject jData=null;
String[][] children = new String[jArray.length()][6];
for(int i=0;i<jArray.length();i++){
jData = jArray.getJSONObject(i);
customer_name=jData.getString("customer_name");
Log.i("JSON ", "customer_name LOG " + customer_name);
cList.add(customer_name);
customer_title=jData.getString("event_title");
Log.i("JSON ", "customer_title LOG " + customer_title);
cList.add(customer_title);
customer_street_address=jData.getString("customer_street_address");
Log.i("JSON ", "customer_Id LOG " + customer_street_address);
cList.add(customer_street_address);
customer_city=jData.getString("customer_city");
Log.i("JSON ", "customer_city LOG " + customer_city);
cList.add(customer_city);
customer_state=jData.getString("customer_state");
Log.i("JSON ", "customer_state LOG " + customer_state);
cList.add(customer_state);
customer_postal_code=jData.getString("customer_postal_code");
Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code);
cList.add(customer_postal_code);
for(int ic = 0; ic < cList.size(); ic++) {
Log.i("jData ", "length " + jData.length());
children[i][ic] = (String) cList.get(ic);
}
Log.i("Child Array", "Children array LOG " + children);
}
}catch(JSONException e1){
Toast.makeText(getBaseContext(), "No customers Found", Toast.LENGTH_LONG).show();
}catch (ParseException e1){
e1.printStackTrace();
}
}
If I understood your code correctly you don't need cList.
Something like that should do the work
String[][] children = new String[jArray.length()][6];
for(int i=0;i<jArray.length();i++){
jData = jArray.getJSONObject(i);
customer_name=jData.getString("customer_name");
Log.i("JSON ", "customer_name LOG " + customer_name);
children[i][0] = customer_name;
customer_title=jData.getString("event_title");
Log.i("JSON ", "customer_title LOG " + customer_title);
children[i][1] = event_title;
customer_street_address=jData.getString("customer_street_address");
Log.i("JSON ", "customer_Id LOG " + customer_street_address);
children[i][2] = customer_street_address;
customer_city=jData.getString("customer_city");
Log.i("JSON ", "customer_city LOG " + customer_city);
children[i][3] = customer_city;
customer_state=jData.getString("customer_state");
Log.i("JSON ", "customer_state LOG " + customer_state);
children[i][4] = customer_state;
customer_postal_code=jData.getString("customer_postal_code");
Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code);
children[i][5] = customer_postal_code;
}
Make sure your JSON data is well-formed to avoid exceptions.
To view children[][] you can just iterate twice on your multidimentional array and do Log.i("MyTag", "Value: " + children[i][j]);
String[][] data = new String[jsonArray.length][];
for(int i = 0; i<jsonArray.length; i++){
data[i] = ((ArrayList)jsonArray).get(i).toArray(new String[((ArrayList)jsonArray).get(i).size])
}
for printing in log
Log.d("array", data);

Categories

Resources