java - API Request: Not working from Android Studio -
whenever try api request the: "http://api.openweathermap.org/data/2.5/weather?q=london"
code fails fetch data. however, working fine other urls. here's main java file. i've taken added necessary permissions in , manifest file.
package thebestone.vicky.jsondemo; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.concurrent.executionexception; public class mainactivity extends appcompatactivity { class getdata extends asynctask<string, void, string>{ @override protected string doinbackground(string... strings) { string result = ""; url url; httpurlconnection urlconnection = null; try{ url = new url(strings[0]); urlconnection = (httpurlconnection)url.openconnection(); inputstream in = urlconnection.getinputstream(); inputstreamreader reader = new inputstreamreader(in); int data = reader.read(); while(data != -1){ char ch = (char) data; result = result + ch; data = reader.read(); } return result; }catch (exception e) { e.printstacktrace(); return "failed"; } } @override protected void onpostexecute(string s) { super.onpostexecute(s); log.i("data fetched: ", s); } } public void onclick(view view){ getdata task = new getdata(); task.execute("http://api.openweathermap.org/data/2.5/weather?q=london"); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
logcat:
08-18 19:05:10.478 2380-4307/thebestone.vicky.jsondemo w/system.err: java.io.filenotfoundexception: http://api.openweathermap.org/data/2.5/weather?q=london 08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo w/system.err: @ com.android.okhttp.internal.huc.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:250) 08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo w/system.err: @ thebestone.vicky.jsondemo.mainactivity$getdata.doinbackground(mainactivity.java:33) 08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo w/system.err: @ thebestone.vicky.jsondemo.mainactivity$getdata.doinbackground(mainactivity.java:19) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ android.os.asynctask$2.call(asynctask.java:305) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ java.util.concurrent.futuretask.run(futuretask.java:237) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ android.os.asynctask$serialexecutor$1.run(asynctask.java:243) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1133) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:607) 08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo w/system.err: @ java.lang.thread.run(thread.java:761) 08-18 19:05:10.481 2380-2380/thebestone.vicky.jsondemo i/data fetched:: failed
also i'm not able figure out error in logcat , program.
any appreciated.
i suppose have got same 401 unauthorized me. if im not mistaken answer @raghunandan should solution.
Comments
Post a Comment