android - Having issues parsing JSON data to TextView the app just crashes -
i cannot anything. once input completed crashes.
the commented out section how displayed information want upc , product displayed. ideas?
if there question out there, please link it.
crash logs:
04-08 21:12:52.416: w/dalvikvm(26817): threadid=12: thread exiting uncaught exception (group=0x40ac4228) 04-08 21:12:52.426: e/androidruntime(26817): fatal exception: thread-48634 04-08 21:12:52.426: e/androidruntime(26817): java.lang.nullpointerexception 04-08 21:12:52.426: e/androidruntime(26817): @ net.example.glutefree.networking.getserverdata(networking.java:113) 04-08 21:12:52.426: e/androidruntime(26817): @ net.example.glutefree.networking.access$0(networking.java:68) 04-08 21:12:52.426: e/androidruntime(26817): @ net.example.glutefree.networking$1.run(networking.java:49) 04-08 21:12:52.727: e/log_tag(26817): result [{"id":"512320","upca":"310742023497","company":"310742","product":"oxy max deep pore pads","gluten free":null}] 04-08 21:12:52.727: w/dalvikvm(26817): threadid=13: thread exiting uncaught exception (group=0x40ac4228) 04-08 21:12:52.737: e/androidruntime_2_crash(26817): crash in same process: thread-48635 04-08 21:12:52.737: e/androidruntime_2_crash(26817): java.lang.nullpointerexception 04-08 21:12:52.737: e/androidruntime_2_crash(26817): @ net.example.glutefree.networking.getserverdata(networking.java:113) 04-08 21:12:52.737: e/androidruntime_2_crash(26817): @ net.example.glutefree.networking.access$0(networking.java:68) 04-08 21:12:52.737: e/androidruntime_2_crash(26817): @ net.example.glutefree.networking$1.run(networking.java:49)
package net.example.glutefree; import android.app.activity; import android.os.bundle; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; import android.widget.edittext; import android.widget.linearlayout; import android.widget.textview; public class networking extends activity{ textview txt; int request_code = 1; //called when activity first created @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_networking); txt = new textview(getapplicationcontext()); // set text , call connect function. txt.settext("connecting..."); //call method run data retreival new thread() { public void run() { final string data = getserverdata(key_121); if (data != null) runonuithread(new runnable() { public void run() { txt.settext(data); } }); } }.start(); } public static final string key_121 = "http://glutefree.com/application_query.php"; private string getserverdata(string returnstring) { string upc = getintent().getstringextra("upca"); inputstream = null; string result = ""; //the upc data send arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("upca",upc)); //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(key_121); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection "+e.tostring()); } //convert response string 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(); result=sb.tostring(); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //parse json data try{ jsonarray jarray = new jsonarray(result); log.e("log_tag", "result "+result.tostring()); for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(i); string upca = json_data.getstring("upca"); string product = json_data.getstring("product"); textview upca = (textview)findviewbyid(r.id.textview1); upca.settext("upca: " + upca); textview product = (textview)findviewbyid(r.id.textview2); product.settext("product: " + product); /* log.i("log_tag","upca: "+json_data.getstring("upca")+ ", product: "+json_data.getstring("product"));*/ //get output screen returnstring += "\n\t" + jarray.getjsonobject(i); } }catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); } return returnstring; } }
xml file:
<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="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=".networking" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="37dp" android:layout_margintop="38dp" android:text="textview" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/textview1" android:layout_below="@+id/textview1" android:layout_margintop="63dp" android:text="textview" /> </relativelayout>
new errors:
04-08 22:27:38.545: e/log_tag(3312): result [{"id":"512320","upca":"310742023497","company":"310742","product":"oxy max deep pore pads","gluten free":null}] 04-08 22:27:38.545: w/dalvikvm(3312): threadid=12: thread exiting uncaught exception (group=0x40aa6228) 04-08 22:27:38.565: e/androidruntime(3312): fatal exception: thread-475 04-08 22:27:38.565: e/androidruntime(3312): android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.viewrootimpl.checkthread(viewrootimpl.java:4381) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.viewrootimpl.requestlayout(viewrootimpl.java:805) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.view.requestlayout(view.java:12887) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.view.requestlayout(view.java:12887) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.view.requestlayout(view.java:12887) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.view.requestlayout(view.java:12887) 04-08 22:27:38.565: e/androidruntime(3312): @ android.widget.relativelayout.requestlayout(relativelayout.java:268) 04-08 22:27:38.565: e/androidruntime(3312): @ android.view.view.requestlayout(view.java:12887) 04-08 22:27:38.565: e/androidruntime(3312): @ android.widget.textview.checkforrelayout(textview.java:7207) 04-08 22:27:38.565: e/androidruntime(3312): @ android.widget.textview.settext(textview.java:3474) 04-08 22:27:38.565: e/androidruntime(3312): @ android.widget.textview.settext(textview.java:3324) 04-08 22:27:38.565: e/androidruntime(3312): @ android.widget.textview.settext(textview.java:3299) 04-08 22:27:38.565: e/androidruntime(3312): @ net.example.glutefree.networking.getserverdata(networking.java:108) 04-08 22:27:38.565: e/androidruntime(3312): @ net.example.glutefree.networking.access$0(networking.java:63) 04-08 22:27:38.565: e/androidruntime(3312): @ net.example.glutefree.networking$1.run(networking.java:44)
ok, logs can see have nullpointerexception on line 113:
textview upca = (textview)findviewbyid(r.id.textview1); //line 112 upca.settext("upca: " + upca); //line 113
this means upca
textview
not initialised in line 112. can check if have textview1
in layout have set above?
update: second error getting viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views.
this happens when trying update ui thread
thread
. doing in code calling getserverdata()
new thread
, while within method trying change text of textviews
in ui thread
.
hope above makes sense?
Comments
Post a Comment