Create table rows dynamically from parsed JSON in AsyncTask in Android -
i kind of stuck right now.i want create table parsed json data.the json fetched webservice using asynctask on click of button.the fetched json parsed within asynctask. want parallely create tabular layout , show on user interface. have included asynctask class. example: json [{"instrument":"eurcad"},{"entry price","1.453"}]
the table should this:
|instrument | entryprice|
|eurcad | 1.453 |
please help!!!
asynctask
package com.shubhamhpcs.fetchdb; import android.os.asynctask; import android.util.log; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; /** * created satyam on 7/11/2016. */ public class fetchinstrumenttask extends asynctask<void,void,string[]> { private final string log_tag = fetchinstrumenttask.class.getsimplename(); //to parse json string recieved server public string[] getinstrumentdatafromjson(string forecastjsonstr) throws jsonexception { string[] resultstrs = new string[12]; final string owm_instrument ="instrument"; final string owm_new_signal ="newsignal"; final string owm_entry_type ="entrytype"; final string owm_entry_price ="entryprice"; final string owm_trailing_stop_1 ="trailingstop1"; final string owm_trailing_stop_2 ="trailingstop2"; final string owm_tgt ="tgt"; final string owm_tgt_hit ="tgthit"; final string owm_p_l ="p&l"; final string owm_stop_loss ="stoploss"; jsonarray instrumentarray = new jsonarray(forecastjsonstr); (int = 0; < instrumentarray.length(); i++) { jsonobject instrumentobject = instrumentarray.getjsonobject(i); string instrumentname = instrumentobject.getstring(owm_instrument); string newsignal = instrumentobject.getstring(owm_new_signal); double entrytype = instrumentobject.getdouble(owm_entry_type); double entryprice = instrumentobject.getdouble(owm_entry_price); double trailingstop1 = instrumentobject.getdouble(owm_trailing_stop_1); double trailingstop2 = instrumentobject.getdouble(owm_trailing_stop_2); double tgt = instrumentobject.getdouble(owm_tgt); string tgthit = instrumentobject.getstring(owm_tgt_hit); string pl = instrumentobject.getstring(owm_p_l); string stoploss = instrumentobject.getstring(owm_stop_loss); resultstrs[i] = instrumentname + "|" + newsignal + " | " + entrytype + " | " + entryprice + "|" + trailingstop1 + "|" + trailingstop2 + "|" + tgt + "|" + tgthit + "|" + pl + "|" + stoploss ; } (string s : resultstrs) { log.v(log_tag, "instrument entry: " + s); } return resultstrs; } @override protected string[] doinbackground(void... voids) { // these 2 need declared outside try/catch // can closed in block. httpurlconnection urlconnection = null; bufferedreader reader = null; // contain raw json response string. string instrumentjsonstr = null; try { url url = new url("http://192.168.1.101/tooth/index.php"); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { return null; } reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) { buffer.append(line + "\n"); } if (buffer.length() == 0) { return null; } instrumentjsonstr = buffer.tostring(); } catch (ioexception e) { log.e("fetchinstrumenttask", "error ", e); return null; } finally{ if (urlconnection != null) { urlconnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final ioexception e) { log.e("fetch", "error closing stream", e); } } } log.v("fetchinstrumenttask ","data vps is: "+instrumentjsonstr); try { return getinstrumentdatafromjson(instrumentjsonstr); } catch (jsonexception e) { log.e(log_tag, e.getmessage(), e); e.printstacktrace(); } return null; } }
mainactivity
package com.shubhamhpcs.fetchdb; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; public class main2activity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); } public void getjson(view view){ fetchinstrumenttask fetchinstrumenttask =new fetchinstrumenttask(); fetchinstrumenttask.execute(); } }
let's make instrument
class, contains instrument name , price, better strings.
your asynctask return list of instruments list<instrument>
, instead of string[]
.
then introduce method onpostexecute
asynctask
, provide data activity. can example make activity implement interface iinstrumentsdisplay
, have method void showinstruments(list<instrument>)
. initialize asynctask reference interface (the activity).
in method showinstruments
in activity construct adapter, example arrayadapter<instrument>
, initialize data returned asynctask
, , point listview
(that display table) adapter.
your activity include listview
in layout xml, need define item row xml layout file, , in adapter override getview() method display name , price in correct place of row.
Comments
Post a Comment