这是一个简单的客户端-服务器安卓应用程序。服务器是在本地 pc 上运行的简单 java 服务器,客户端在模拟器上运行。这是客户的代码:
package com.example.streamer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
class Connecting implements Runnable
{
private Socket sock;
private BufferedReader r;
private BufferedWriter out;
public Connecting ()
{
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
System.out.println("************************ trying to initiated *****************");
sock = new Socket("10.0.2.2",12344);
System.out.println("************************ socket initiated *****************");
r = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println("************************ buffer reader initiated *****************");
out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
System.out.println("************************ buffer writer initiated *****************");
}
catch (IOException ioe) { }
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onResume() {
super.onResume();
System.out.println("************************ on resume *****************");
Connecting c = new Connecting();
}
}
任何人都知道为什么该指令sock = new Socket("10.0.2.2", 12344);
没有执行?