我開發了一個使用套接字編程的聊天應用程序,有1個客戶端和1個服務器。 我正在運行2個項目。 1個用於客戶端,1個用於服務器。 我的客戶端應用程序安裝在模擬器上,但當我點擊圖標時,它不運行。 有沒有錯誤:我的應用程序獲取安裝在Android模擬器上,但它無法運行,並且沒有錯誤
package com.client.chat;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
import java.net.*;
import java.io.*;
public class ClientActivity extends Activity{
/** Called when the activity is first created. */
private String serverIpAddress = "10.0.2.2";
private static final int TCP_SERVER_PORT = 6000;
private Button bt;
private Socket s;
private TextView tv;
String error;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// bt=(Button)findViewById(R.id.cbtn);
tv=(TextView)findViewById(R.id.clienttext);
tcpclient();
}
private void tcpclient(){
try{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
s = new Socket(serverAddr, TCP_SERVER_PORT);
}catch(UnknownHostException e){e.printStackTrace(); error=e.toString();
tv.setText(error);
}catch(IOException e){e.printStackTrace();
error=error.toString();
tv.setText(error);
}
try{
EditText et = (EditText) findViewById(R.id.clientedit);
String str = et.getText().toString();
BufferedWriter out = null;
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String outMsg=str+System.getProperty("line.separator");
// String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(str);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
Log.d("Client", "Client sent message");
}catch(UnknownHostException e){e.printStackTrace();
error=e.toString();
tv.setText(error);
}catch(IOException e){e.printStackTrace();
error=e.toString();
tv.setText(error);
}
//accept server response
BufferedReader in=null;
try{
in= new BufferedReader(new InputStreamReader(s.getInputStream()));
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
}catch(UnknownHostException e){error=e.toString();
tv.setText(error);
}catch(IOException e){error=e.toString();
tv.setText(error);
}
finish();
}
}
請幫忙!
嘗試運行「亞行logcat」,或查看logcat的標籤,如果你是從Eclipse中運行,這可能會給你一些有用的信息 – crobicha 2012-02-01 19:55:07