JSON data wont load into my android app -


i need advice on manipulating json data. i've come absolute conclusion there wrong json data using blog.

i'm feeding json data android application display information on app. problem having when use json data negativearraysize exception.

now before blame code, know got app work fine using teamtreehouse blog json code work here:

http://blog.teamtreehouse.com/api/get_recent_summary/

so works fine. made json code code. arrays nested same, same naming conventions used... etc..

here jsoncode brings exception when plugged in android code.

http://www.evotechmachine.com/api/get_recent_posts/?include=title,url,status,id,date

if compare 2 json codes you'll see pretty same on inside. thing reference in either posts, title, , url. i'm sure not mismatch of names. i'm @ total loss. in advance people.

public static final int number_of_posts = 20; public static final string tag = mainlistactivity.class.getsimplename(); protected jsonobject mblogdata; protected progressbar mprogressbar; private final string key_title = "title"; private final string key_author = "author"; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main_list);      mprogressbar = (progressbar) findviewbyid(r.id.progressbar1);       if (isnetworkavailable()) {     mprogressbar.setvisibility(view.visible);         getblogpoststask getblogpoststask = new getblogpoststask();     getblogpoststask.execute();     }     else {toast.maketext(this, "network unavailable", toast.length_long).show();      }     // toast.maketext(this, message, toast.length_long).show(); } @override protected void onlistitemclick(listview l, view v, int position, long id) {         super.onlistitemclick(l, v, position, id);         jsonarray jsonposts;         try {             jsonposts = mblogdata.getjsonarray("posts");                         jsonobject jsonpost = jsonposts.getjsonobject(position);             string  blogurl = jsonpost.getstring("url");             intent intent = new intent(this, blogwebviewactivity.class);             intent.setdata(uri.parse(blogurl));             startactivity(intent);         } catch (jsonexception e) {             logexception(e);         }  } private void logexception(exception e) {     log.e(tag, "exception caught!", e); }  private boolean isnetworkavailable() {     connectivitymanager manager = (connectivitymanager) getsystemservice(context.connectivity_service);      networkinfo networkinfo = manager.getactivenetworkinfo();      boolean isavailable = false;     if (networkinfo != null && networkinfo.isconnected()) {         isavailable = true;     }     return isavailable;     }   @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } public void handleblogresponse() {     mprogressbar.setvisibility(view.invisible);     if(mblogdata==null) {         updatedisplayforerror();     }     else {         try {          jsonarray jsonposts = mblogdata.getjsonarray("posts");         arraylist<hashmap<string, string>> blogposts =                  new arraylist<hashmap<string, string>>();           (int = 0;i< jsonposts.length();i++) {              jsonobject post = jsonposts.getjsonobject(i);              string title = post.getstring(key_title);              title = html.fromhtml(title).tostring();              string author = post.getstring(key_author);              author = html.fromhtml(author).tostring();                        hashmap<string, string> blogpost = new hashmap<string, string>();              blogpost.put(key_title, title);              blogpost.put(key_author, author);               blogposts.add(blogpost);           }           string[] keys= { key_title, key_author };          int[] ids = {android.r.id.text1, android.r.id.text2};          simpleadapter adapter = new simpleadapter(this, blogposts, android.r.layout.simple_list_item_2, keys, ids);          setlistadapter(adapter);         } catch (jsonexception e) {             logexception(e);         }     }  }   private void updatedisplayforerror() {     alertdialog.builder builder = new alertdialog.builder(this);     builder.settitle(getstring(r.string.error_title));     builder.setmessage(getstring(r.string.error_message));      builder.setpositivebutton(android.r.string.ok, null);     alertdialog dialog = builder.create();     dialog.show();      textview emptytextview = (textview) getlistview().getemptyview();     emptytextview.settext(getstring(r.string.no_items)); }     private class getblogpoststask extends asynctask<object, void, jsonobject> {      @override     protected jsonobject doinbackground(object... arg0) {         int responsecode = -1;         jsonobject jsonresponse = null;         try {             url blogfeedurl = new url("http://www.evotechmachine.com/api/get_recent_posts/?include=title,url,status,id,date");             httpurlconnection connection = (httpurlconnection) blogfeedurl.openconnection();             connection.connect();              responsecode = connection.getresponsecode();             if (responsecode == httpurlconnection.http_ok){                 inputstream inputstream = connection.getinputstream();                 reader reader = new inputstreamreader(inputstream);                 int contentlength = connection.getcontentlength();                 char[] chararray = new char[contentlength];                 reader.read(chararray);                 string responsedata = new string(chararray);                  jsonresponse = new jsonobject(responsedata);              } else {                 log.i(tag, "unsuccessful http response code: " +responsecode);             }             log.i(tag, "code: " + responsecode);         } catch (malformedurlexception e) {              logexception(e);}     catch (ioexception e) {         logexception(e);         }         catch (exception e) {             logexception(e);         }                  return jsonresponse;     }      @override     protected void onpostexecute(jsonobject result) {         mblogdata = result;          handleblogresponse();     } } 

}

here's logcat:

07-31 20:49:08.209: e/(32544): <s3dreadconfigfile:75>: can't open file reading 07-31 20:49:08.209: e/(32544): <s3dreadconfigfile:75>: can't open file reading 07-31 20:49:09.280: e/mainlistactivity(32544): exception caught! 07-31 20:49:09.280: e/mainlistactivity(32544): java.lang.negativearraysizeexception: -1 07-31 20:49:09.280: e/mainlistactivity(32544):  @ com.nibbdigital.blogreader.mainlistactivity$getblogpoststask.doinbackground(mainlistactivity.java:168) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ com.nibbdigital.blogreader.mainlistactivity$getblogpoststask.doinbackground(mainlistactivity.java:1) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ android.os.asynctask$2.call(asynctask.java:287) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ java.util.concurrent.futuretask.run(futuretask.java:137) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 07-31 20:49:09.280: e/mainlistactivity(32544):  @ java.lang.thread.run(thread.java:856) 

your content-length negative (i.e. unknown)

check out insights

https://stackoverflow.com/a/5428670/770467

hope helps


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -