Question

the following is my code. Editor: Eclipse, Platform: Windows.

Its a chat application where 2 android emulators connect through a tcp socket.

UI consists of a send button, a text view and a text box.

Problem: As soon as I type text and hit send, the application crashes.

server port is 8000. So my redirection is redir add tcp:8081:8000 and redir add tcp:8082:8000.

I donno what is wrong in my code. Please suggest me somthing I need to change.

public class HelloandroidActivity extends Activity 
{
/** Called when the activity is first created. */
public int serverport=8000;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   final EditText nameField = (EditText) findViewById(R.id.editText1);             
   final Button button2 = (Button) findViewById(R.id.button1);  
   Integer severport=8000;
   new Server().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,severport);
   button2.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
          final String name = nameField.getText().toString();
          final TextView tv = (TextView) findViewById(R.id.textView1);
          //tv.setText(name);

          String s=null;

        new Client().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,s);
     } 
          });// end onclicklis  

     }//end oncreate



 class Server extends AsyncTask <Integer, String, String>
{
public InetAddress byIpAsName ;
int r=0;
@Override
protected String doInBackground(Integer... serverport)  {
    //i[0]=serverport;
    Integer[] sp=serverport;
    BufferedReader in=null;
    ServerSocket s=null;
     r=sp[0];
    String cIn="";
    try {
        //byIpAsName = InetAddress.getByName("10.2.2.15");
        s=new ServerSocket(r);
    while(true)
    {
        Socket client = s.accept();
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String line=in.readLine();
        cIn=null;
        while(line!=null){cIn=cIn.concat(line);}

    }//while    

    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        s.close();
        in.close();
        } 
    catch (IOException e) {
        e.printStackTrace();
    }
    return cIn;


}//end inBackground
//@SuppressWarnings("null")
protected void onPostExecute(String... cIn)
{

  }//onpost execute

  }//server class
    public class Client extends AsyncTask<String, String, String>
    {
     PrintWriter out = null;
    BufferedReader in=null;
    String sIn=null;
     //Server s1=new Server();
    //int q=s1.r;
    TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() - 4);
    int q = Integer.parseInt(portStr);
   Socket socket;
   @Override
   protected String doInBackground(String... params) {
   try 
   {
     //q=8080;
     InetAddress byIpAsName1=InetAddress.getByName("10.0.2.2");
     socket = new Socket(byIpAsName1, q);
     out = new PrintWriter(socket.getOutputStream(), true);
     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String line=in.readLine();
      sIn=null;
     while(line!=null){sIn=sIn.concat(line);}
    } 
    catch (IOException e) {
    e.printStackTrace();
    }//catch
   return sIn;
    }//in background
    protected void onPostExecute(String... sIn)
    {
    String c=null;
    final TextView tv = (TextView) findViewById(R.id.textView1);
   c=c.concat(sIn[0]);
   tv.setText(c);
    }
     }  

  }//main class
Was it helpful?

Solution

From your logcat, what is important is this line:

03-16 23:12:23.434: E/AndroidRuntime(571): java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10040 nor current process has android.permission.READ_PHONE_STATE.

This indicates that in order to run your code, you need the READ_PHONE_STATE permission in the android manifest.xml.

Add this line to the manifest, outside of the <application> tag but inside the <manifest> tag.

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

If this does not solve the issue, the problem could be related to this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top