从Java服务器接收文件到Android。accept()不工作
大家好,我正在创建简单的交互式android应用程序。服务器运行Java。 事实证明,我无法从服务器接收文件 Android代码如下从Java服务器接收文件到Android。accept()不工作,android,sockets,android-asynctask,connection,Android,Sockets,Android Asynctask,Connection,大家好,我正在创建简单的交互式android应用程序。服务器运行Java。 事实证明,我无法从服务器接收文件 Android代码如下 public class MainActivity extends Activity { public DataReceiving dataReceiving; public DataTransfer dataTransfer; private EditText inputData; private Button sendParameters; private
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;
}
似乎套接字传入=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>
我将非常感谢任何想法,如何解决这个问题。谢谢我认为客户端部分工作正常,它停止在accept方法。因此,问题出在永远无法连接的服务器上。 服务器部分应如下所示:
try {
Socket s = new Socket();
InetAddress addr = InetAddress.getByName("192.168.1.110");
SocketAddress sockaddr = new InetSocketAddress(addr, 8888);
s.connect(sockaddr, 20000); // 20 seconds time out
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ArrayLIst b = new ArrayList();
b.add("It Works");
oos.writeObject(b);
oos.close();
s.close();
}
catch (Exception e)
{
.....
}
可以肯定的是,您可以从服务器使用telnet 192.168.1.110 8888吗?你能接通吗?如果无法建立连接,请检查防火墙,暂时禁用防火墙,然后重试telnet。
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();
try {
Socket s = new Socket();
InetAddress addr = InetAddress.getByName("192.168.1.110");
SocketAddress sockaddr = new InetSocketAddress(addr, 8888);
s.connect(sockaddr, 20000); // 20 seconds time out
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ArrayLIst b = new ArrayList();
b.add("It Works");
oos.writeObject(b);
oos.close();
s.close();
}
catch (Exception e)
{
.....
}