Populate Listview With Json Parser
i want to populate listview with json parser. but i have following error: 12-12 22:49:39.812: ERROR/JSON Parser(1254): Error parsing data org.json.JSONException: Value
Solution 1:
Error parsing data org.json.JSONException: Value <!DOCTYPE oftypejava.lang.String cannotbeconvertedtoJSONObject
Looks like what you get is a String and you are trying to convert it into a JSONObject
You do a HttpPost
instead of HttGet
HttpPosthttpPost=newHttpPost(url);
Should be
HttpGethttpget=newHttpGet(url);
Your json
{// json object node "NewItem":[// json array NewItem{// json object node "name":"Roast Ground Coffee",// string"description":"Folgers Medium Classic Roast Ground Coffee 339 oz","price":8},
Second Mistake
catch (Exception ex){
Toast.makeText(getApplicationContext(),"network problem",Toast.LENGTH_SHORT).show();
returnnull;
Displaying toast in doInbackground
. you cannot update ui indoInbackground
Complete Example with Snap Shot:
test.java
publicclasstestextendsActivity {
ListView list;
Button Btngetdata;
ProgressDialog pDialog;
ArrayList<HashMap<String, String>> newItemlist = newArrayList<HashMap<String, String>>();
privatestaticfinalStringTAG_ITEM="NewItem";
privatestaticfinalStringTAG_NAME="name";
privatestaticfinalStringTAG_DESCRIPTION="description";
privatestaticfinalStringTAG_PRICE="price";
@OverrideprotectedvoidonCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.test);
pDialog = newProgressDialog(test.this);
pDialog.setMessage("Getting Data ...");
newItemlist = newArrayList<HashMap<String, String>>();
Btngetdata = (Button)findViewById(R.id.button1);
Btngetdata.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
newJSONParse().execute();
}
});
}
privateclassJSONParseextendsAsyncTask<String, Void, Void> {
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
pDialog.show();
}
@Overrideprotected Void doInBackground(String... args) {
try {
Log.i("...............","Hello..............");
DefaultHttpClienthttpClient=newDefaultHttpClient();
HttpGethttpPost=newHttpGet("http://www.karocellen.com/newItem.json");
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= httpResponse.getEntity();
Stringjsonstring= EntityUtils.toString(httpEntity);
Log.i("...............",jsonstring);
JSONObjectjson=newJSONObject(jsonstring);
JSONArraynewitem= json.getJSONArray(TAG_ITEM);
for(inti=0; i < newitem.length(); i++){
JSONObjectc= newitem.getJSONObject(i);
Stringname= c.getString(TAG_NAME);
Stringdescription= c.getString(TAG_DESCRIPTION);
Stringprice= c.getString(TAG_PRICE);
Log.i("...............",name);
HashMap<String, String> map = newHashMap<String, String>();
map.put(TAG_NAME, name);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_PRICE, price);
newItemlist.add(map);
}
} catch (Exception ex){
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
list=(ListView)findViewById(R.id.list);
ListAdapteradapter=newSimpleAdapter(test.this, newItemlist,
R.layout.second,
newString[] { TAG_NAME,TAG_DESCRIPTION, TAG_PRICE }, newint[] {
R.id.textView1,R.id.textView2, R.id.textView3});
list.setAdapter(adapter);
}
}
}
test.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/button1"
/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:text="Button" /></RelativeLayout>
second.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="16dp"android:text="TextView" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/textView1"android:layout_marginTop="50dp"android:text="TextView" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView2"android:layout_below="@+id/textView2"android:layout_marginTop="56dp"android:text="TextView" /></RelativeLayout>
Snap
Solution 2:
Your page is not generating the JSON correctly,
"<!DOCTYPE"
seems you are feeding in some HTML before outputting your JSON string.
Solution 3:
Change your json response should be for price like...
{
"NewItem": [
{
"name": "Roast Ground Coffee",
"description": "Folgers Medium Classic Roast Ground Coffee 339 oz",
"price": "8"
}
]
}
get price valueasstringand then you can cast it into (Integer value) in your code.
Post a Comment for "Populate Listview With Json Parser"