Reading a json url file in Android returns blank screen on my phone - nothing's displayed on phone -


this first post on stackoverflow , trying learn android. need read json file within android application.

i ran following code on phone , doesn't display on phone, blank screen.

i know there ways use java libraries parse json that's not looking for. accomplish reading json using tools provided android. here's json

i using android 4.0.3

json file data:

{     "offers": [         {                 "id": "1",                 "type":"coupon",                 "description":"half off @ gap",                 "url":"http://www.gap.com/"         },         {                 "id": "1",                 "type":"offer",                 "description":"buy 1 1 @ ann taylor",                 "url":"http://www.anntaylor.com/"         },         {                 "id": "1",                 "type":"type:promotion",                 "description":"free kids scoop first ten people",                 "url":"http://www.gap.com/"         }     ] } 

mainactivity.java

package com.pega.parsejson;  import java.io.ioexception;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.view.menu; import android.widget.textview; import android.widget.toast;  public class mainactivity extends activity {      textview jsonwrapper;     httpclient httpclient;     jsonobject json;       final static string offersurl="http://somewebsite.com/offers.json";        @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         jsonwrapper=(textview) findviewbyid(r.id.jsonwrap);         httpclient=new defaulthttpclient();         new read().execute("description");     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      public jsonobject jsonoffers() throws clientprotocolexception, ioexception, jsonexception {         //stringbuilder url=new stringbuilder(offersurl);          httpget get=new httpget(offersurl);         httpresponse res=httpclient.execute(get);         int status=res.getstatusline().getstatuscode();          if(status==200){             httpentity e=res.getentity();             string data=entityutils.tostring(e);             jsonarray timeline=new jsonarray("offers");             jsonobject jsonoffer=timeline.getjsonobject(0); //returns recent offer             return jsonoffer;         }else{             toast.maketext(getapplicationcontext(), "error", toast.length_short).show();             return null;         }      }      public class read extends asynctask<string, integer, string>{          @override         protected string doinbackground(string... params) {             try {                 json=jsonoffers();                 return (string) json.get("description"); //returns string w/ parameter passed in "description"             } catch (clientprotocolexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              return null;         }          @override         protected void onpostexecute(string result) {             jsonwrapper.settext(result);         }      }  } 

i using internet permissions:

manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.pega.parsejson"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="8"         android:targetsdkversion="17" />     <uses-permission android:name="android.permission.internet"/>      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.pega.parsejson.mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     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/jsonwrap"         android:layout_width="match_parent"         android:layout_height="400dp"         android:text="" />  </relativelayout> 

logcat:

[2013-08-07 23:35:41 - parsejson] ------------------------------ [2013-08-07 23:35:41 - parsejson] android launch! [2013-08-07 23:35:41 - parsejson] adb running normally. [2013-08-07 23:35:41 - parsejson] performing com.pega.parsejson.mainactivity activity launch [2013-08-07 23:35:42 - parsejson] uploading parsejson.apk onto device 'ht25ehx00690' [2013-08-07 23:35:42 - parsejson] installing parsejson.apk... [2013-08-07 23:35:45 - parsejson] success! [2013-08-07 23:35:45 - parsejson] starting activity com.pega.parsejson.mainactivity on device ht25ehx00690 [2013-08-07 23:35:46 - parsejson] activitymanager: starting: intent { act=android.intent.action.main cat=[android.intent.category.launcher] cmp=com.pega.parsejson/.mainactivity } 



please note not want use json libraries parse json, strictly use android parse json. don't why mainactivity.java doesn't display on phone.

apologize length of post, need on time sensitive project , yes, have read other similar questions on stackoverflow , none seem answer question.

could please me on this?

thanks

try this:

jsonobject jobj; jsonarray data = null; jsonobject jsonoffer;      jobj = new jsonobject(data);     data = jobj.getjsonarray("offers");     jsonoffer=data.getjsonobject(0); 

hope helps!!


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -