Parsing Json from URL in Android Studio -
i trying parse json. have lately been attempting use gson in android studio. however, can't seem obtain connection or data. data looking at, if ever obtain site this:
{"item":{"icon":"address_here", id=820","id":820,"type":"default", "typeicon":"catagory","name":"name of object", "description":"this annoying issue stuck on"}}
i have been using async, no avail. perhaps new way @ good.
here's current code:
public class javaparser extends asynctask<string, string, string> { public string getinternetdata() throws exception { string surl = "someurl.com"; //just string // connect url using java's native library try { url url = new url(surl); httpurlconnection request = (httpurlconnection) url.openconnection(); log.d("debug:", "made partial connection"); try { request.connect(); } catch (ioexception e1) { e1.printstacktrace(); } log.d("debug:", "made connection"); } catch(exception e){ } return "failure"; } private string response = null; @override protected string doinbackground(string...params) { try { response = new javaparser().getinternetdata(); } catch(exception e){ log.d("error",e.getmessage() ); } return response; }
}
in main class:
string response = new javaparser().execute("url").get(); //eventually hope pass url here more dynamic //convert json object print data jsonparser jp = new jsonparser(); //from gson jsonelement root = jp.parse(new inputstreamreader((inputstream) request.getcontent())); //convert input stream json element jsonobject rootobj = root.getasjsonobject(); //may array, may object. gson gson = new gson(); wrapper response = gson.fromjson(rootobj, wrapper.class); log.d("out: ",rootobj.get("item\name").getasstring()); //just grab name info
and wrapper looks so:
public class wrapper { @serializedname("name") public string item_name; @serializedname("icon") public string iconurl; @serializedname("description") public string item_description; //more added if works
}
the program ever makes partial connection. cannot seem line request.connect(); function whatsoever.
as turns out, permission in wrong part of manifest. needs near top, put such
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yargonauts.burk.app"> <uses-permission android:name="android.permission.internet" />
Comments
Post a Comment