java - Weather api connect crash -
i'm trying current wind speed openweathermap api. tried couple of ways current weather geographic coordinates. it's first time i'm using api, , can't understand why keeps crashing. if can me, great. :)
lat/long current location.
public string getwind(string lat, string lng) {// string w_speed = "0"; try { url url = null; url = new url(c.weather_api + "lat=" + (int) double.parsedouble(lat) + "&lon=" + (int) double.parsedouble(lng)); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); try { urlconnection.setdoinput(true); urlconnection.setdooutput(true); inputstreamreader in = new inputstreamreader(urlconnection.getinputstream()); bufferedreader br = new bufferedreader(in); string result = br.readline(); return w_speed; } catch (malformedurlexception e) { e.printstacktrace(); } { urlconnection.disconnect(); } } catch (ioexception e) { e.printstacktrace(); } return null; }
according openweathermap json format i'm geting api.
{"coord":{"lon":139,"lat":35},"sys":{"message":1.473,"country":"jp","sunrise":1429905589,"sunset":1429953850},"weather":[{"id":800,"main":"clear","description":"sky clear","icon":"01n"}],"base":"stations","main":{"temp":288.106,"temp_min":288.106,"temp_max":288.106,"pressure":1026.09,"sea_level":1034.25,"grnd_level":1026.09,"humidity":100},"wind":{"speed":4.55,"deg":11.0004},"clouds":{"all":0},"dt":1429989748,"id":1851632,"name":"shuzenji","cod":200}
with stacktrace :
04-26 00:31:59.320 8686-8686/com.matchrace.matchrace w/system.err﹕ android.os.networkonmainthreadexception 04-26 00:31:59.325 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145) 04-26 00:31:59.330 8686-8686/com.matchrace.matchrace w/system.err﹕ @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 04-26 00:31:59.330 8686-8686/com.matchrace.matchrace w/system.err﹕ @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 04-26 00:31:59.330 8686-8686/com.matchrace.matchrace w/system.err﹕ @ java.net.inetaddress.getallbyname(inetaddress.java:214) 04-26 00:31:59.330 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.dns$1.getallbyname(dns.java:28) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.routeselector.resetnextinetsocketaddress(routeselector.java:216) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.routeselector.next(routeselector.java:122) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:292) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.sendsocketrequest(httpengine.java:255) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:206) 04-26 00:31:59.335 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:345) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:296) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:179) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.matchrace.matchrace.mainactivity.getwind(mainactivity.java:157) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.matchrace.matchrace.mainactivity.onlocationchanged(mainactivity.java:202) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.location.locationmanager$listenertransport._handlemessage(locationmanager.java:279) 04-26 00:31:59.340 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.location.locationmanager$listenertransport.access$000(locationmanager.java:208) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.location.locationmanager$listenertransport$1.handlemessage(locationmanager.java:224) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:102) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.os.looper.loop(looper.java:136) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ android.app.activitythread.main(activitythread.java:5034) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ java.lang.reflect.method.invokenative(native method) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:515) 04-26 00:31:59.345 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 04-26 00:31:59.350 8686-8686/com.matchrace.matchrace w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:611) 04-26 00:31:59.350 8686-8686/com.matchrace.matchrace w/system.err﹕ @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132) 04-26 00:31:59.350 8686-8686/com.matchrace.matchrace w/system.err﹕ @ dalvik.system.nativestart.main(native method)
use asynctask or network library such volley, loopj etc following example of asynctask
class longopreation extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { string str = ""; try { str = sendgetrequest(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } return str; } public string sendgetrequest() throws malformedurlexception { stringbuilder response = new stringbuilder(); string requrl = ""; requrl = c.weather_api + "lat=" + (int) double.parsedouble(lat) + "&lon=" + (int) double.parsedouble(lng); response = requestexecuter(requrl); return response.tostring(); } @override protected void onpostexecute(string result) { try { jsonobject jsonobject = new jsonobject(result); system.out.println("json-----------------------"+jsonobject); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpreexecute() { } public stringbuilder requestexecuter(string str) { stringbuilder response = new stringbuilder(); try { url url = new url(str); httpurlconnection httpconn = (httpurlconnection) url .openconnection(); httpconn.setconnecttimeout(5000); httpconn.setreadtimeout(10000); if (httpconn.getresponsecode() == httpurlconnection.http_ok) { bufferedreader input = new bufferedreader( new inputstreamreader(httpconn.getinputstream())); string strline = null; while ((strline = input.readline()) != null) { response.append(strline); } input.close(); } } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return response; } }
you can pass parameter asynctask , call asynctask below
new longoperation().execute("");//with empty string parameter
Comments
Post a Comment