Wednesday, May 30, 2018

java - GET data from NPM json-server throws NetworkOnMainThreadException

Im coding a little Android App which should get Data from a npm json-server. It throws an exception. I did extend the connection class with an AsyncTask and executed the code in "doInBackground" but it still throws an exception. Please Help me.



Here's the code:



import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Network extends AsyncTask {
public String urlString = ("http://localhost:3000");


@Override
protected String doInBackground(String... params) {
StringBuffer chaine = new StringBuffer("");
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agnet", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();


InputStream inputStream = connection.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}

} catch (IOException e) {

e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();

}
return chaine.toString();
}
}



logcat is telling:




android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)

at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:361)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at smoca.ch.kreagen.Network.doInBackground(Network.java:25)
at smoca.ch.kreagen.BackendMockup.getData(BackendMockup.java:11)
at smoca.ch.kreagen.FillRealm.initRealm(FillRealm.java:43)
at smoca.ch.kreagen.Fragments.HomeFragment.onCreateView(HomeFragment.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)

at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)




I know the problem is when "connection.connect();" gets called because its supposed to be in the main-Thread. But shouldn't. right?



Greetings John

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...