0
I’ve tried everything, using the HttpURLConnection
, with the HttpClient
that is deprecated, these two options do not end, stop at the BufferedReader
. I’ve tried to use AsyncTask
with the doInBackground
, but of error:
android.os.Networkonmainthreadexception
I have already put the internet access permission. Follows below what I am trying to do.
try {
URL url = new URL(s);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
System.out.println("----------------------"+stringBuilder.toString());
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
if given Networkonmainthreadexception then asynctask is wrong...this error occurs just when trying to connect to the UI thread (briefly to Activity).
– Mr_Anderson
You have some more error information?
– viana
You used Asynctask the wrong way if you made the mistake
NetworkOnMainThreadException
– Loudenvier