2012-03-16 43 views
1

以下是我的代码。编辑器:Eclipse,平台:Windows。使用tcp套接字连接android模拟器

其中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 
+0

你可以发布错误的logcat吗? – 2012-03-16 22:55:51

+0

@John,谢谢你的回复..以下链接是logcat – BudsNanKis 2012-03-16 23:17:45

+0

https://docs.google.com/a/buffalo.edu/document/d/10VFYUL94revSdxSxrCmGnOFTZRNqvu7rdu1bx0iCAPY/edit – BudsNanKis 2012-03-16 23:17:54

回答

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> 

如果不解决这个问题,这个问题可能与this answer

+0

你是对的。感谢您指出。 – BudsNanKis 2012-03-20 05:04:33

+0

如果我的回答是正确的,请不要忘记通过点击绿色复选标记来接受它(您将获得代表)!此外,如果您认为某个答案或问题有帮助,您也可以点击左侧的向上箭头来加入它。欢迎来到StackOverflow! – 2012-03-20 16:54:15

+0

嗨,约翰,这里是新的logcat,我不知道那里到底发生了什么,这个文件是非常巨大的..看到并让我知道如果你发现任何值得注意的事情。让我知道。谢谢。 https://docs.google.com/a/buffalo.edu/document/d/1hN-V0qL2qtzUtRCKnaslUweN62G3mxLrusM0fjI18L4/edit – BudsNanKis 2012-03-20 19:07:36