java - org.json.JSONObject cannot be converted to JSONArray -
i unable display values coming in form of json response
and failed convert these values , display textview..
my json reponse :
{"calculate":{"persons":"148429","cost":25232.93}}
and main activity java file :
import android.app.progressdialog; import android.os.asynctask; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.textview; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; public class mainactivity extends actionbaractivity { textview uid; textview name1; textview email1; button btngetdata; //url json array private static string url = "http://urls.p/getdata.php?"; //json node names private static final string tag_user = "calculate"; //private static final string tag_id = "id"; private static final string tag_name = "persons"; private static final string tag_email = "cost"; jsonarray user = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btngetdata = (button)findviewbyid(r.id.getdata); btngetdata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new jsonparse().execute(); } }); } private class jsonparse extends asynctask<string, string, jsonobject> { private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); uid = (textview)findviewbyid(r.id.uid); name1 = (textview)findviewbyid(r.id.name); email1 = (textview)findviewbyid(r.id.email); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("getting data ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { jsonparser jparser = new jsonparser(); // getting json url jsonobject json = jparser.getjsonfromurl(url); return json; } @override protected void onpostexecute(jsonobject json) { pdialog.dismiss(); (int = 0; < json.length(); i++) { try { //jsonobject c = json.getjsonobject(i); // getting json array jsonobject c = user.getjsonobject(i); // storing json item in variable // string id = c.getstring(tag_id); //system.out.println(); string name = c.getstring(tag_name); // system.out.println(name); string email = c.getstring(tag_email); //system.out.println(email); //set json data in textview //uid.settext(id); name1.settext(name); email1.settext(email); } catch (jsonexception e) { e.printstacktrace(); } } } } }
and json parser class:
import android.util.log; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; /** * created admin on 09-04-2015. */ public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url) { // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
activity_main.xml file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/uid" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#ff000000" /> <textview android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/name" android:textappearance="?android:attr/textappearancelarge" android:layout_alignright="@+id/getdata" android:layout_alignend="@+id/getdata" android:textcolor="#ff000000" /> <textview android:id="@+id/uid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_alignparenttop="true" android:textcolor="#ff000000" /> <button android:id="@+id/getdata" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="23dp" android:text="get data" android:layout_gravity="center_horizontal" /> </linearlayout>
and logcat:
04-09 09:04:34.904 1170-1170/com.example.health.myapplication w/system.err﹕ org.json.jsonexception: value {"cost":25232.93,"persons":"148429"} @ calculate of type org.json.jsonobject cannot converted jsonarray
your json jsonobject not jsonarray.
{
represents jsonobject node
[
represents jsonarray node
so having for (int = 0; < json.length(); i++) {
wrong
in onpostexecute
jsonobject calculate = json.optjsonobject("calculate"); string person = calculate.optstring("persons"); string cost = calculate.optstring("cost");