1

以下是我的代码。编辑器:Eclipse,平台:Windows。

它是一个聊天应用程序,其中 2 个 android 模拟器通过 tcp 套接字连接。

UI 由发送按钮、文本视图和文本框组成。

问题:只要我输入文本并点击发送,应用程序就会崩溃。

服务器端口是 8000。所以我的重定向是 redir add tcp:8081:8000 和 redir add tcp:8082:8000。

我不知道我的代码有什么问题。请给我一些我需要改变的建议。

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
4

1 回答 1

1

从您的 logcat 中,重要的是这一行:

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.

这表明为了运行您的代码,您需要 android manifest.xml 中的 READ_PHONE_STATE 权限。

将此行添加到清单中,在<application>标签之外但在<manifest>标签内。

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

如果这不能解决问题,则问题可能与此答案有关。

于 2012-03-19T17:12:13.020 回答