0
I’m running a method on android, where I try to perform a recover some information from a webservice ... I stopped to debug and whenever it will connect it falls in the catch and when it continues it goes up to the Activity Thread class, in which it presents a lot of errors as it is possible to identify in the following image
I am using the following method for connection
public String acessar(String endereco) throws IOException {
URL url = new URL("http://pokeapi.co/api/v2/pokemon/1/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
if (inputStream == null) {
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String linha;
StringBuffer buffer = new StringBuffer();
while ((linha = reader.readLine()) != null) {
buffer.append(linha + "\n");
}
if (buffer.length() == 0) {
return null;
}
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("Erro", "Erro fechando o stream", e);
}
}
return "";
}
And even though having all the Try and catch it still for my application, what would be the best method to fix this problem? I also enabled all access ha internet in relation to the manifests.
Error that appears:
08-21 13:06:37.016 3240-3240/dev.sbruno.sibcom E/Androidruntime: FATAL EXCEPTION: main Process: dev.sbruno.sibcom, PID: 3240 java.lang.Runtimeexception: Unable to start Activity Componentinfo{dev.sbruno.sibcom/dev.sbruno.sibcom.Listacategoriaactivity}: android.os.Networkonmainthreadexception at android.app.Activitythread.performLaunchActivity(Activitythread.java:2665) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2726) at android.app.Activitythread. -wrap12(Activitythread.java) at android.app.Activitythread$H.handleMessage(Activitythread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:886) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:776) Caused by: android.os.Networkonmainthreadexception at android.os.Strictmode$Androidblockguardpolicy.onNetwork(Strictmode.java:1303) at java.net.Inet6addressimpl.lookupHostByName(Inet6addressimpl.java:86) at java.net.Inet6addressimpl.lookupAllHostAddr(Inet6addressimpl.java:74) at java.net.Inetaddress.getAllByName(Inetaddress.java:752) at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(Routeselector.java:187) at com.android.okhttp.internal.http.RouteSelector.nextProxy(Routeselector.java:156) at com.android.okhttp.internal.http.RouteSelector.next(Routeselector.java:98) at com.android.okhttp.internal.http.HttpEngine.createNextConnection(Httpengine.java:345) at com.android.okhttp.internal.http.HttpEngine.connect(Httpengine.java:328) at com.android.okhttp.internal.http.HttpEngine.sendRequest(Httpengine.java:246) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(Httpurlconnectionimpl.java:457) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(Httpurlconnectionimpl.java:126) at dev.sbruno.sibcom.ListaCategoriaActivity.access(Listcategoriaactivity.java:76) at dev.sbruno.sibcom.ListaCategoriaActivity.onCreate(Listacategoriaactivity.java:63) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2618) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2726) at android.app.Activitythread. -wrap12(Activitythread.java) at android.app.Activitythread$H.handleMessage(Activitythread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:886) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:776)
You could post the stack trace of the error, I think it would help more. I recommend you use the RETROFIT library to handle this type of request as it makes it easy.
– Diniz
To add this RETORFIT to my project just add it to the Compile, or I have to download something and add?
– Bruno Aparecido da Silva
@Brunoasimilarjust add to the project, but gives a studied before on how it works. I would also recommend Spring Android, which I think is a little simpler than Retrofit. There’s Volley too, which is from Google itself.
– Grupo CDS Informática
I edited stating the error that appears
– Bruno Aparecido da Silva
I love when someone comes across a problem and asks for help the suggestions are: "use the XY API instead".
– ramaral
I could not solve my problem, so I started using Retrofit, in a tutorial I did on the internet worked 100%, but in my local machine there are some errors!. here is the link of the new post, if you can help me I am grateful [https://answall.com/questions/230794/problema-com-retorno-do-retrofit-em-android]
– Bruno Aparecido da Silva