大家好,我正在创建简单的交互式 android 应用程序。服务器运行 Java。事实证明,我无法从服务器接收文件。
Android代码如下
public class MainActivity extends Activity {
public DataReceiving dataReceiving;
public DataTransfer dataTransfer;
private EditText inputData;
private Button sendParameters;
private Button startComputation;
public TextView displayText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputData= (EditText) findViewById(R.id.editText1);
sendParameters=(Button) findViewById(R.id.button1);
startComputation=(Button) findViewById(R.id.button2);
displayText=(TextView) findViewById(R.id.textView1);
startComputation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String numberOfGates=inputData.getText().toString();
ArrayList send=new ArrayList();
send.add(numberOfGates);
dataTransfer=new DataTransfer();
dataTransfer.execute(send);
}
});
sendParameters.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dataReceiving=new DataReceiving();
dataReceiving.execute();
}
});
}
public class DataReceiving extends AsyncTask<Void, Void, ArrayList>
{
@Override
protected ArrayList doInBackground(Void... params) {
ArrayList receivedData=new ArrayList();
Log.i("DataReceiving", "doInbackgroung works");
try {
receivedData=receive();
} catch (ClassNotFoundException e) {
Log.e("DataReceiving", "Problems with receive method. Issue with class");
e.printStackTrace();
} catch (IOException e) {
Log.e("DataReceiving", "Problems with receive method. Issue with IO");
e.printStackTrace();
}
return receivedData;
}
@Override
protected void onPostExecute(ArrayList result) {
super.onPostExecute(result);
displayText.setText(result.toString());
}
}
public ArrayList receive () throws IOException, ClassNotFoundException
{
Log.i("receive method", "works");
ServerSocket s= new ServerSocket(8888);
Log.i("receive method", "ServerSocket(8888)");
Socket incoming =s.accept();
Log.i("receive method", "accept()");
ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());
Log.i("receive method", "ios");
ArrayList b=new ArrayList();
b = (ArrayList) ios.readObject();
Log.i("receive method", "data were received");
ios.close();
incoming.close();
s.close();
return b;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
这是日志
06-18 18:08:51.709: D/dalvikvm(2160): GC_CONCURRENT freed 69K, 7% free 2760K/2952K, paused 78ms+85ms, total 261ms
06-18 18:08:52.079: D/gralloc_goldfish(2160): Emulator without GPU emulation detected.
06-18 18:09:05.090: I/DataReceiving(2160): doInbackgroung works
06-18 18:09:05.099: I/receive method(2160): works
06-18 18:09:05.099: I/receive method(2160): ServerSocket(8888)
似乎 Socket 传入 =s.accept(); 不起作用,我不知道为什么
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
服务器端很简单
Socket s=new Socket("192.168.1.110", 8888);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ArrayLIst b = new ArrayList();
b.add("It Works");
oos.writeObject(b);
oos.close();
s.close();
我将非常感谢任何想法,如何解决这个问题。谢谢