0

I have made a client in my android app that connect to pc server. When my app connect to server i want to see "Connection established" , for this i'm using toast but my program crash at startup. Here there's the code:

 protected String doInBackground(String... params) {

       try {
             Socket client = new Socket("192.168.1.2", 4444);  //connect to server
             Toast t = Toast.makeText(getApplicationContext(), "Connection established" ,Toast.LENGTH_LONG);
             t.show();
             client.close();   //closing the connection

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

        }

        return null;
    }

i have this problem since i've written "Toast t = Toast.makeText(getApplicationContext(), "Connection established" ,Toast.LENGTH_LONG);"

here there's the logcat:

    05-29 16:28:54.076: E/AndroidRuntime(32436): FATAL EXCEPTION: AsyncTask #1
05-29 16:28:54.076: E/AndroidRuntime(32436): java.lang.RuntimeException: An error occured while executing doInBackground()
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.lang.Thread.run(Thread.java:841)
05-29 16:28:54.076: E/AndroidRuntime(32436): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.os.Handler.<init>(Handler.java:197)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.os.Handler.<init>(Handler.java:111)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.widget.Toast$TN.<init>(Toast.java:324)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.widget.Toast.<init>(Toast.java:91)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.widget.Toast.makeText(Toast.java:238)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at com.example.social_network.MainActivity$myTask.doInBackground(MainActivity.java:34)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at com.example.social_network.MainActivity$myTask.doInBackground(MainActivity.java:1)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-29 16:28:54.076: E/AndroidRuntime(32436):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-29 16:28:54.076: E/AndroidRuntime(32436):    ... 4 more

thanks

codeMagic
  • 44,549
  • 13
  • 77
  • 93
xXJohnRamboXx
  • 739
  • 3
  • 10
  • 24
  • Please do not change your OP this makes it confusing for others and the answers obsolete. If you have a new problem then please post a new question with relevant information. I have rolled it back to the OP so it is correct. – codeMagic May 29 '14 at 14:50
  • Do yourself a favor and follow the link to the dupe and look at @Aman's answer. You are making it more difficult than it needs to be and introducing more potential for errors. – codeMagic May 29 '14 at 15:00

3 Answers3

2

Of course, you need to use best practices with AsyncTask. For example check this post.

But if you want just to test it quickly, try this:

Replace these lines:

Toast t = Toast.makeText(getApplicationContext(), "Connection established", Toast.LENGTH_LONG);
t.show();

by these ones:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    public void run() {
        Toast t = Toast.makeText(getApplicationContext(), "Connection established", Toast.LENGTH_LONG);
        t.show();
    }
});

FOR UPDATED CODE:

Replace this line:

if(result.equalsIgnoreCase("Exception Caught")){

by this one:

if ("Exception Caught".equalsIgnoreCase(result)) {
Community
  • 1
  • 1
Oleksii K.
  • 5,359
  • 6
  • 44
  • 72
1

Toast can not be shown in dodoInBackground() because its related to UI and you can not perform UI related task in doInBackground().You can use that in onPostExecute() or onPreExecute().

AsyncTask

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Aman Systematix
  • 347
  • 2
  • 10
  • 1
    Don't forget `onProgressUpdate()` but +1 for giving the best solution instead of suggesting unnecessary things which could lead to more problems. – codeMagic May 29 '14 at 14:54
0

Take a look at this: How to show toast in AsyncTask in doInBackground

You cannot call Toast in doinbackground. You should call it in onPostExecute() or onProgressUpdate().

Edit thanks to codeMagic.

Community
  • 1
  • 1
Zoran
  • 1,484
  • 1
  • 10
  • 13
  • What solution did you do? runOnUiThread or moving Toast to onPostExecute? – Zoran May 29 '14 at 14:48
  • 1
    I would not recommend suggesting to use `runOnUiThread()` with `AsyncTask`. The class already has methods to handle any need for this and it can just lead to more problems. – codeMagic May 29 '14 at 14:52