java - How to get a String from HTTP and use it to build a JSON Object? -
i try string (a json file in reality) http server store data twitch stream.
the string got connecting : api.twitch.tv/kraken/streams/lainkk
i string in string variable , build json object string.
i wrote methods got systemerr in log while calling methods:
this own twitch api class :
package com.linkpulsion.bibix; import android.util.log; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstreamreader; import java.net.url; import java.net.urlconnection; public class twitch { private string channel; public final static string twitch_api_server = "https://api.twitch.tv/kraken/"; /** * constructor of java twitch api json generator * @param channel cannel name infos */ public twitch(string channel){ if(channel != null){ this.channel = channel; } } /** * getter channel name * @return string channel name */ public string getchannel(){ return this.channel; } protected string getjson(string mode){ string apiuri = twitch_api_server + mode + "/" + this.channel; try{ url website = new url(apiuri); urlconnection connection = website.openconnection(); bufferedreader in = new bufferedreader(new inputstreamreader(connection.getinputstream())); stringbuilder response = new stringbuilder(); string inputline; while ((inputline = in.readline()) != null) response.append(inputline); in.close(); return response.tostring(); }catch (exception e){ e.printstacktrace(); return null; } } protected jsonobject buildjson(string jsonraw){ jsonobject json = null; try{ json = new jsonobject(jsonraw); }catch (exception e){ e.printstacktrace(); } return json; } public void issreaming(){ string jsonraw = getjson("streams"); log.d(null,"retour " + jsonraw); } }
thank !
i ran original code , got networkonmainthreadexception.
i made simple asynctask, , worked:
private class twitchasync extends asynctask<void, void, string> { @override protected string doinbackground(void... params) { string str = twitch.getjson("streams"); return str; } @override protected void onpostexecute(string result){ toast.maketext(mainactivity.this, "result: " + result, toast.length_long).show(); //testing buildjson: jsonobject obj = twitch.buildjson(result); log.d("json result: ", obj.tostring()); } }
here's full class used test with:
public class mainactivity extends appcompatactivity { twitch twitch; jsonobject jsonobj; //instance variable can accessed anywhere in class code @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); twitch = new twitch("aksynial"); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { system.out.println("refreshing"); new twitchasync().execute(); return true; } return super.onoptionsitemselected(item); } private class twitchasync extends asynctask<void, void, string> { @override protected string doinbackground(void... params) { string str = twitch.getjson("streams"); return str; } @override protected void onpostexecute(string result){ toast.maketext(mainactivity.this, "result: " + result, toast.length_long).show(); //testing buildjson: //assign instance variable jsonobj jsonobj = twitch.buildjson(result); log.d("json result: ", jsonobj.tostring()); } } }
log result in onpostexecute()
:
d/json result:﹕ {"_links":{"channel":"https:\/\/api.twitch.tv\/kraken\/channels\/aksynial","self":"https:\/\/api.twitch.tv\/kraken\/streams\/aksynial"},"stream":null}
Comments
Post a Comment