nick28
nick28

Reputation: 67

How to implement asyncTask for a socket connection

I saw an online example of how to impelement a socket connection between an android app and a java server on a pc. The app runs perfectly on the emulator (running 2.3.3) but it force closes on my android smartphone.

After some research I concluded that the solution is to implement an asynctask method. But i'm having problems knowing hot to implement it. I saw many explanations but they didn't help me with my case. This is the sample code that I'm using, I'm going to use this code as a reference template for any further apps I develop.

I know the question has been asked alot (alot alot!), but till now I haven't managed to make this work. So can anybody help me with the following code:

package com.exercise.AndroidClient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     textOut = (EditText)findViewById(R.id.textout);
     Button buttonSend = (Button)findViewById(R.id.send);
     textIn = (TextView)findViewById(R.id.textin);
     buttonSend.setOnClickListener(buttonSendOnClickListener);
 }

 Button.OnClickListener buttonSendOnClickListener
 = new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
 // TODO Auto-generated method stub
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
  socket = new Socket("192.168.1.101", 8888);
  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  dataInputStream = new DataInputStream(socket.getInputStream());
  dataOutputStream.writeUTF(textOut.getText().toString());
  textIn.setText(dataInputStream.readUTF());
 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 finally{
  if (socket != null){
   try {
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataOutputStream != null){
   try {
    dataOutputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataInputStream != null){
   try {
    dataInputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 ;
}};
}

LogCat

03-03 21:35:59.643: E/Trace(3472): error opening trace file: No such file or directory (2)
03-03 21:36:01.073: D/gralloc_goldfish(3472): Emulator without GPU emulation detected.
03-03 21:36:12.045: D/AndroidRuntime(3472): Shutting down VM
03-03 21:36:12.045: W/dalvikvm(3472): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-03 21:36:12.103: E/AndroidRuntime(3472): FATAL EXCEPTION: main
03-03 21:36:12.103: E/AndroidRuntime(3472): android.os.NetworkOnMainThreadException
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at libcore.io.Streams.readFully(Streams.java:81)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.io.DataInputStream.readShort(DataInputStream.java:169)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:182)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.io.DataInputStream.readUTF(DataInputStream.java:186)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at com.exercise.AndroidClient.AndroidClient$LongOperation.onPostExecute(AndroidClient.java:80)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at com.exercise.AndroidClient.AndroidClient$LongOperation.onPostExecute(AndroidClient.java:1)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.os.Looper.loop(Looper.java:137)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 21:36:12.103: E/AndroidRuntime(3472):     at dalvik.system.NativeStart.main(Native Method)
03-03 21:36:14.722: I/Process(3472): Sending signal. PID: 3472 SIG: 9

Upvotes: 0

Views: 5453

Answers (3)

WassiM ZgheiB
WassiM ZgheiB

Reputation: 119

@nick28: you have an uncaught exception on line 45, what is your line 45? try the following codes before initialising your controls, hopefully your application will not close :

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Upvotes: 0

greenapps
greenapps

Reputation: 11214

As Neil said before: you have to do all with the socket in doInBackgroud. Also receiving the string.

Upvotes: 0

Neil Townsend
Neil Townsend

Reputation: 6084

I'm assuming that the Activity has the right permissions to open a socket.

I would guess that the problem with running the code is that you are making an internet connection on the UI thread. This is heavily discouraged, and sometimes simply results in the application stopping.

You are correct that you need to create an AsyncTask with your code and trigger it. Two places to learn about this are: How to execute web request in its own thread? and Is there an accepted best-practice on making asynchronous HTTP requests in Android?

Upvotes: 1

Related Questions