2

It seems to me I m not really calling URL,network operations right from MainActivity.

  • I do call url.connect() in another class ProcessData
  • I do use doInBackground()
  • I do extend ProcessData class with AsyncTask

but the exception still occurs:

public class MainActivity extends ActionBarActivity {

    private final String LOG_TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {            
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ProcessData test = new ProcessData();
        test.doInBackground("mysql") ;
    }

//separate file ProcessData.java
public class ProcessData extends AsyncTask<String, Void, String> {

public String createValidUrl(String s){
       .....//code in this method works fine
}
public String doInBackground(String ...params){
        String validUrl = createValidURL(params[0]);                   
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        URL url = new URL(validUrl);
        urlConnection = (HttpURLConnection) url.openConnection();        
        urlConnection.setRequestMethod("GET");            
        urlConnection.connect();           

        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();

        reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;
            while((line = reader.readLine()) != null) {

                buffer.append(line + "\n");
            }
            urlConnection.disconnect();
            reader.close();

            return buffer.toString();
}
}

The network main thread exception still occurs, i read other SO posts. I can wrap my http connection code into new thread, but I want to find exactly what is wrong

ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • Post the stacktrace and the line to which it points. – nhaarman Mar 28 '15 at 17:22
  • @NiekHaarman, 03-28 16:35:45.086: E/AndroidRuntime(1798): FATAL EXCEPTION: main 03-28 16:35:45.086: E/AndroidRuntime(1798): at com.android.okhttp.internl.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) 03-28 16:35:45.086: E/AndroidRuntime(1798): at com.example.kaban.it_ebooks2.ProcessData.doInBackground(ProcessData.java:130) basically, it is the line in doInbackground with inputstream = getinputstream() – ERJAN Mar 28 '15 at 17:30

2 Answers2

5

You have to call the execute method on an AsyncTask in order to run it in a background thread. What you're doing is calling doInBackground directly from the main thread.

Jan Van den bosch
  • 3,542
  • 3
  • 26
  • 38
3

You have to call

test.execute();

instead of

test.doInBackground("mysql") ;
Ajit Pratap Singh
  • 1,299
  • 12
  • 24
  • so execute() is IMPLICIT method that includes doInbackground followed by onPostExecute() ? is this how android works? – ERJAN Mar 28 '15 at 17:33
  • @ERJAN - Yes you have to call execute .It will take care of calling the onPreExecute(), then doInBackground() and then onPostExecute(). What is your first comment?Can you post in comments again. – Ajit Pratap Singh Mar 30 '15 at 04:03