1

The main class calls an asynch task. The asynch task calls 2 procedures. The first one to parse an XML file and get an html link. The second one to read through that html file. Pasted the asynch task code and the second procedure which reads through an html file (Don't think the first procedure causes problems) I get 'Force Close' exceptions even after handling every http exception and setting socket timeouts. If I call the readfile procedure separately I get a sockettimeout exception on my mobile (if the connection is slow). But if I call it from within the asynch task I get Force Close exceptions.

1) Why am I getting a sockettimeout exception in my logcat even though I've handled the exception? 2) I'm passing the exception error message back to the asynctask. But still it's not getting displayed. Should I apply a try-catch in async task procedure also?

Please help.

Regards, Sam

           private class SelectDataTask extends AsyncTask<String, Void, List<String> {  
           private final ProgressDialog dialog = new ProgressDialog(MessageList.this);  
           // can use UI thread here  
          @Override  
           protected void onPreExecute() {  
            dialog.setMessage("Fetching scores...");  
            dialog.show();
         }  


         // automatically done on worker thread (separate from UI thread)
         @Override
         protected List<String> doInBackground(final String... args) {
                  List<String> scores = new ArrayList<String>();
                  List<String> scores1 = new ArrayList<String>();
                  scores =  loadFeed() ;

                  //return loadFeed();
                  for (String sco : scores) {
                      scores1.add(readFile(sco));
                  }
                  return scores1;  

         }


         // can use UI thread here
        @Override
         protected void onPostExecute(final List<String> result) {
            if (dialog.isShowing()) {
               dialog.dismiss();
            }
           adapter = 
                  new ArrayAdapter<String>(MessageList.this,R.layout.row,result);
           MessageList.this.setListAdapter(adapter);

         }
      }

      private String readFile(String htmllink){
       String TAG = "myActivity";
      List<String> scores = new ArrayList<String>();
      String result="";
      String score = "";
      int CONNECTION_TIMEOUT_MS = 30 * 1000; // ms

      HttpParams my_httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(my_httpParams,CONNECTION_TIMEOUT_MS); 
      HttpConnectionParams.setSoTimeout(my_httpParams, CONNECTION_TIMEOUT_MS); 
      HttpClient client = new DefaultHttpClient(my_httpParams);  // get http client with given params 
      HttpGet request = new HttpGet(htmllink);
      try{
             HttpResponse response = client.execute(request);
             //txtResult.setText(HttpHelper.request(response));
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                 try{
                     InputStream in = response.getEntity().getContent();
                     BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                     StringBuilder str = new StringBuilder();
                     String line = null;
                     while((line = reader.readLine()) != null){
                         str.append(line + "\n");
                     }
                     in.close();

                     result = str.toString();
                     //result = str.toString().substring(1,500);

                     Pattern p = Pattern.compile(
                             "<title>(.*)</title>",
                              Pattern.DOTALL
                         );
                     Matcher matcher = p.matcher(
                              result
                         );

                     if (matcher.find())
                     {
                          score = matcher.group(1).toString();

                     }

                     return score;

                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     Toast.makeText(this, "IOException e  = " + e.toString(), Toast.LENGTH_LONG).show();
                     Log.d(TAG, "IOException e  = " + e.toString());
                        client.getConnectionManager().shutdown();
                     return "IOException e  = " + e.toString();
                  }catch(Exception ex){
                     result = "Error";
                     ex.printStackTrace();
                     Toast.makeText(this, "Exception ex  = " + ex.toString(), Toast.LENGTH_LONG).show();
                     Log.d(TAG,"Exception ex  = " + ex.toString());
                        client.getConnectionManager().shutdown();     
           return "Exception ex  = " + ex.toString();
                 }
             } else {
                    client.getConnectionManager().shutdown();
                 return   "Error connecting" + response.getStatusLine().toString();
                 //sin.close();
             } //httpstatus
          } catch (SocketTimeoutException e) {
             e.printStackTrace();
             Toast.makeText(this, "SocketTimeoutException e  = " + e.toString(), Toast.LENGTH_LONG).show();
             Log.d(TAG, "SocketTimeoutException e  = " + e.toString());
             client.getConnectionManager().shutdown();
             return "SocketTimeoutException e  = " + e.toString();
          }catch(Exception ex){
              Toast.makeText(this, "Exception ex1  = " + ex.toString(), Toast.LENGTH_LONG).show();
                Log.d(TAG, "Exception ex1  = " + ex.toString());
                client.getConnectionManager().shutdown();
                return  "Exception ex1  = " + ex.toString();
         }

Logcat error messages

          W/System.err(18422): java.net.SocketTimeoutException
      W/System.err(18422):  at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
      W/System.err(18422):  at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
      W/System.err(18422):  at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
      W/System.err(18422):  at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
      W/System.err(18422):  at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
      W/System.err(18422):  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
      W/System.err(18422):  at java.io.InputStreamReader.read(InputStreamReader.java:275)
      W/System.err(18422):  at java.io.BufferedReader.fillBuf(BufferedReader.java:155)
      W/System.err(18422):  at java.io.BufferedReader.readLine(BufferedReader.java:425)
      W/System.err(18422):  at com.warriorpoint.androidxmlsimple.MessageList.readFile(MessageList.java:214)
      W/System.err(18422):  at com.warriorpoint.androidxmlsimple.MessageList.access$1(MessageList.java:182)
      W/System.err(18422):  at com.warriorpoint.androidxmlsimple.MessageList$SelectDataTask.doInBackground(MessageList.java:158)
      W/System.err(18422):  at com.warriorpoint.androidxmlsimple.MessageList$SelectDataTask.doInBackground(MessageList.java:1)
      W/System.err(18422):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
      W/System.err(18422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
      W/System.err(18422):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
      W/System.err(18422):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
      W/System.err(18422):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
      W/System.err(18422):  at java.lang.Thread.run(Thread.java:1096)
      W/dalvikvm(18422): threadid=7: thread exiting with uncaught exception (group=0x400207d8)
      E/AndroidRuntime(18422): FATAL EXCEPTION: AsyncTask #1
      E/AndroidRuntime(18422): java.lang.RuntimeException: An error occured while executing doInBackground()
      E/AndroidRuntime(18422):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
      E/AndroidRuntime(18422):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
      E/AndroidRuntime(18422):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
      E/AndroidRuntime(18422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
      E/AndroidRuntime(18422):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
      E/AndroidRuntime(18422):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
      E/AndroidRuntime(18422):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
      E/AndroidRuntime(18422):  at java.lang.Thread.run(Thread.java:1096)
      E/AndroidRuntime(18422): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
      E/AndroidRuntime(18422):  at android.os.Handler.<init>(Handler.java:121)
      E/AndroidRuntime(18422):  at android.widget.Toast.<init>(Toast.java:68)
      E/AndroidRuntime(18422):  at android.widget.Toast.makeText(Toast.java:231)
      E/AndroidRuntime(18422):  at com.warriorpoint.androidxmlsimple.MessageList.readFile(MessageList.java:276)
      E/AndroidRuntime(18422):  at com.warriorpoint.androidxmlsimple.MessageList.access$1(MessageList.java:182)
      E/AndroidRuntime(18422):  at com.warriorpoint.androidxmlsimple.MessageList$SelectDataTask.doInBackground(MessageList.java:158)
      E/AndroidRuntime(18422):  at com.warriorpoint.androidxmlsimple.MessageList$SelectDataTask.doInBackground(MessageList.java:1)
      E/AndroidRuntime(18422):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
      E/AndroidRuntime(18422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
      E/AndroidRuntime(18422):  ... 4 more
      W/ActivityManager(  158):   Force finishing activity com.warriorpoint.androidxmlsimple/.MessageList

1 Answer 1

1

The SocketTimeoutException is not cause for the forced close, the exception stack trace is just printed to log because you ask it to be printed (e.printStackTrace()).

This is the real problem:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

To resolve the real problem, look here: Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog

Sign up to request clarification or add additional context in comments.

2 Comments

The issue is because I'm printing to the log? How do I resolve this issue now?
No, the issue has nothing to do with the SocketTimeoutException. I just meant the SocketTimeoutException stacktrace shows first on the log because you print it there but the reason your app gets killed is the exception printer after it to the log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.