I want to receive an xml from webservice on android

Asked

Viewed 68 times

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).

  • You have some more error information?

  • You used Asynctask the wrong way if you made the mistake NetworkOnMainThreadException

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.