android - How do I parse the url in the given code to read as per the output defined in the code? -


i new android , want parse url using json. exceptions when run code , application says unfortunately myapp has stopped running. have posted logcat. want read
course code:101,
course title : "blahblah"
semesters offered : semester 1, semester 2, semester 3

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import java.util.arraylist;  import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.responsehandler; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.basichttpparams; import org.apache.http.params.httpparams; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.app.listactivity; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.textview; import android.widget.toast;  public class compsci734mainactivity extends listactivity {      asynctask<void, void, void> mtask;     string jsonstring;      //string url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=50cent&count=2";      string url = "http://redsox.tcs.auckland.ac.nz/734a/csservice.svc/courses";      //button b;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_compsci734_main);         //getactionbar().setdisplayhomeasupenabled(true);          button button = (button) findviewbyid(r.id.btnfetch);         final textview tv = (textview) findviewbyid(r.id.txtview);          mtask = new asynctask<void, void, void> () {             @override             protected void doinbackground(void... params) {                 try {                     jsonstring = getjsonfromserver(url);                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 return null;             }              @override             protected void onpostexecute(void result) {                 super.onpostexecute(result);                  tv.settext(jsonstring);              }         };  //      button button =(button) findviewbyid(r.id.btnfetch); //        textview tv =(textview) findviewbyid(r.id.txtview);         button.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 try {                     string url = "http://redsox.tcs.auckland.ac.nz                                                   /734a/csservice.svc/courses";                     httppost httppost = new httppost(url);                     try {                         httpparams p = new basichttpparams();                         httpclient httpclient = new defaulthttpclient(p);                         responsehandler<string> responsehandler = new                                                         basicresponsehandler();                         string responsebody = httpclient.execute(httppost,                                 responsehandler);                         jsonarray jarray = new jsonarray(responsebody);                         string text="";                         (int = 0; < jarray.length(); i++) {                             jsonobject e = jarray.getjsonobject(i);                                 text = text + "course id :                                         "+e.getstring("coursefield")+"\n";                                 text = text + "course name :                                          "+e.getstring("titlefield")+"\n";                                 text = text + "semesters offered:                                       "+e.getstring("semesterfield")+"\n";                                 //text = text + "birthyear :                                 //        "+e.getstring("birthyear")+"\n";                         }                         log.i(responsebody, text);                         tv.settext(text);                     } catch (clientprotocolexception e) {                         e.printstacktrace();                     } catch (ioexception e) {                         e.printstacktrace();                     }                  } catch (throwable t) {                     toast.maketext(getbasecontext(), "request failed: " +                                         t.tostring(), toast.length_long).show();                     t.printstacktrace();                 }                       //tv.settext(result);             }         });     }       public static string getjsonfromserver(string url) throws ioexception {          bufferedreader inputstream = null;          url jsonurl = new url(url);         urlconnection dc = jsonurl.openconnection();          dc.setconnecttimeout(5000);         dc.setreadtimeout(5000);          inputstream = new bufferedreader(new inputstreamreader(                 dc.getinputstream()));          // read json results string         string jsonresult = inputstream.readline();         return jsonresult;     }      } 

here logcat:

05-13 16:27:16.375: i/dalvikvm(1040): threadid=3: reacting signal 3 05-13 16:27:16.585: e/dalvikvm(1040): unable open stack trace file '/data/anr/traces.txt': directory 05-13 16:27:16.585: d/androidruntime(1040): shutting down vm 05-13 16:27:16.595: w/dalvikvm(1040): threadid=1: thread exiting uncaught exception (group=0x409c01f8) 05-13 16:27:16.605: e/androidruntime(1040): fatal exception: main 05-13 16:27:16.605: e/androidruntime(1040): java.lang.runtimeexception: unable start activity componentinfo{com.example.compsci_734p/com.example.compsci_734p.compsci734mainactivity}: java.lang.runtimeexception: binary xml file line #7: must supply layout_width attribute. 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1956) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread.access$600(activitythread.java:123) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread$h.handlemessage(activitythread.java:1147) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.os.handler.dispatchmessage(handler.java:99) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.os.looper.loop(looper.java:137) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread.main(activitythread.java:4424) 05-13 16:27:16.605: e/androidruntime(1040):     @ java.lang.reflect.method.invokenative(native method) 05-13 16:27:16.605: e/androidruntime(1040):     @ java.lang.reflect.method.invoke(method.java:511) 05-13 16:27:16.605: e/androidruntime(1040):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 05-13 16:27:16.605: e/androidruntime(1040):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 05-13 16:27:16.605: e/androidruntime(1040):     @ dalvik.system.nativestart.main(native method) 05-13 16:27:16.605: e/androidruntime(1040): caused by: java.lang.runtimeexception: binary xml file line #7: must supply layout_width attribute. 05-13 16:27:16.605: e/androidruntime(1040):     @ android.content.res.typedarray.getlayoutdimension(typedarray.java:491) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.viewgroup$layoutparams.setbaseattributes(viewgroup.java:5318) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.viewgroup$marginlayoutparams.<init>(viewgroup.java:5439) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.widget.linearlayout$layoutparams.<init>(linearlayout.java:1776) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.widget.linearlayout.generatelayoutparams(linearlayout.java:1700) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.widget.linearlayout.generatelayoutparams(linearlayout.java:56) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.layoutinflater.rinflate(layoutinflater.java:741) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.layoutinflater.inflate(layoutinflater.java:489) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.layoutinflater.inflate(layoutinflater.java:396) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.view.layoutinflater.inflate(layoutinflater.java:352) 05-13 16:27:16.605: e/androidruntime(1040):     @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:251) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activity.setcontentview(activity.java:1835) 05-13 16:27:16.605: e/androidruntime(1040):     @ com.example.compsci_734p.compsci734mainactivity.oncreate(compsci734mainactivity.java:50) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activity.performcreate(activity.java:4465) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 05-13 16:27:16.605: e/androidruntime(1040):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1920) 05-13 16:27:16.605: e/androidruntime(1040):     ... 11 more 05-13 16:27:16.885: i/dalvikvm(1040): threadid=3: reacting signal 3 05-13 16:27:16.885: i/dalvikvm(1040): wrote stack traces '/data/anr/traces.txt' 05-13 16:27:17.155: i/dalvikvm(1040): threadid=3: reacting signal 3 05-13 16:27:17.165: i/dalvikvm(1040): wrote stack traces '/data/anr/traces.txt' 

you have in xml without layout_width. must have layout_width , layout_height.

also, see next problem- networkonmainthreadexception. you're performing http request in onclick. can't that- need on thread or in asynctask.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -