here application when enter train no 17603 edit text , enter button. correct it, traindetails.txt assets folder, , convert string pass string in json array. showed in logcat. here not getting error.
json requset stored in .txt place in assets folder
{ "status":"ok", "result":{ "trainno":"17603", "route":[ { "code":"kcg", "name":"kacheguda", "arr":"first", "dep":"21:00", "day":1, "stop":"y", "dts":"2.5" } ] } }
main activity
package com.example.trainroutes; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.toast; public class mainactivity extends actionbaractivity implements onclicklistener { private edittext train_search_edittext; private button submit_button; private listview train_listview; // private string jsonstirng ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); train_search_edittext = (edittext) findviewbyid(r.id.trainsearch); submit_button = (button) findviewbyid(r.id.search_train_button); train_listview = (listview) findviewbyid(r.id.trian_name_listview); submit_button.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub if (train_search_edittext.gettext().tostring().equals("17603")) { try { // reading text file assets folder stringbuffer stringbuffer = new stringbuffer(); bufferedreader bufferreader = null; try { bufferreader = new bufferedreader(new inputstreamreader( getassets().open("traindetails.txt"))); string temp; while ((temp = bufferreader.readline()) != null) { stringbuffer.append(temp); } } catch (ioexception e) { e.printstacktrace(); } { try { bufferreader.close(); } catch (ioexception e) { e.printstacktrace(); } } string jsonstirng = stringbuffer.tostring(); log.e("json string", "json string"+jsonstirng); // creating jsonobject string jsonobject jsonmainobj = new jsonobject(jsonstirng); // creating json array json object jsonarray jsonarrayobject = jsonmainobj.getjsonarray("route"); (int = 0; < jsonarrayobject.length(); i++) { jsonobject jsonobject = jsonarrayobject.getjsonobject(i); // getting data individual object string code = jsonobject.getstring("code"); string name = jsonobject.getstring("name"); string arr = jsonobject.getstring("arr"); string dep = jsonobject.getstring("dep"); int day = jsonobject.getint("day"); string stop = jsonobject.getstring("stop"); string dts = jsonobject.getstring("dts"); log.d("jsonobject", "train " + name.tostring()); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } else { toast.maketext(getapplicationcontext(), "invalid ", toast.length_short).show(); } } }
here how should do
string jsonstring = loadjsonfromasset(getapplicationcontext()); try { jsonobject jsonmainobj = new jsonobject(jsonstring); jsonobject jsonarrayobjectresult = jsonmainobj.getjsonobject("result"); jsonarray jsonarrayobjectroute = jsonarrayobjectresult.getjsonarray("route"); (int = 0; < jsonarrayobjectroute.length(); i++) { jsonobject jsonobject = jsonarrayobjectroute.getjsonobject(i); // getting data individual object string code = jsonobject.getstring("code"); string name = jsonobject.getstring("name"); string arr = jsonobject.getstring("arr"); string dep = jsonobject.getstring("dep"); int day = jsonobject.getint("day"); string stop = jsonobject.getstring("stop"); string dts = jsonobject.getstring("dts"); log.d("jsonobject", "train " + name.tostring()); } } catch (jsonexception e) { e.printstacktrace(); } public string loadjsonfromasset(context mcontext) { string json = null; try { inputstream = mcontext.getassets().open("test.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new string(buffer, "utf-8"); } catch (exception e) { e.printstacktrace(); } return json; }
Comments
Post a Comment