我有一个Android应用程序,我想让它能够通过Wi-Fi p2p比较数据。 我理解连接方法(1.设置应用程序权限;设置广播接收器和P2P管理器;启动对等恢复;连接到对等) 但是在数据传输方面我遇到了问题。我的应用程序界面有一些下拉菜单 有几个选项可供选择。我想知道,我如何制作所选的选项(一旦应用程序用户相互连接) 能够比较数据并通知应用程序用户是否有相互选择 这段代码会起作用吗?
//SERVER
public static class FileServerAsyncTask extends AsyncTask {
private Context context;
private TextView statusText;
public FileServerAsyncTask(Context context, View statusText) {
this.context = context;
this.statusText = (TextView) statusText;
}
@Override
protected String doInBackground(Void... params) {
try {
/**
* Create a server socket and wait for client connections. This
* call blocks until a connection is accepted from a client
*/
ServerSocket serverSocket = new ServerSocket(8888);
Socket client = serverSocket.accept();
/**
* If this code is reached, a client has connected and transferred data
*/
var pGender:String
var pDesire:String
var pAge:String
var pRace:String
var pWeight:String
var pHeight:String
var dGender:String = dGender.getValue(dGender.selectedItem.data);
var pDesire:String = pDesire.getValue(pDesire.selectedItem.data);
var dRace:String = dRace.getValue(dRace.selectedItem.data);
var dAge:String = dAge.getValue(dAge.selectedItem.data);
var dHeight:String = dHeight.getValue(dHeight.selectedItem.data);
var dWeight:String = dWeight.getValue(dWeight.selectedItem.data);
if (pGender == dGender.selectedItem.data && pDesire == pDesire.selectedItem.data && pRace == dRace.selectedItem.data && pWeight == dWeight.selectedItem.data && pHeight == dHeight.selectedItem.data)
{
trace (jump to frame 110 + vibrate);
}
else
{
trace (jump to frame 138 + vibrate + jump to frame 10) ;
}
return results;
}
InputStream inputstream = client.getInputStream();
copyFile(inputstream, new FileOutputStream(f));
serverSocket.close();
return f.getAbsolutePath();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
return null;
}
}
/**
* Start activity that can handle the JPEG image
*/
@Override
protected void onPostExecute(String result) {
if (result != null) {
statusText.setText("File copied - " + result);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + result), "image/*");
context.startActivity(intent);
}
}
}
然后在客户端上我有这个:
//CLIENT
Context context = this.getApplicationContext();
String host;
int port;
int len;
Socket socket = new Socket();
byte buf[] = new byte[1024];
...
try {
/**
* Create a client socket with the host,
* port, and timeout information.
*/
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)), 500);
/**
* Create a byte stream from a JPEG file and pipe it to the output stream
* of the socket. This data will be retrieved by the server device.
*/
OutputStream outputStream = socket.getOutputStream();
ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
/*varibles declaration
*/
pGender.getValue(pGender.selectedItem.data);
pDesire.getValue(pDesire.selectedItem.data);
pRace.getValue(pRace.selectedItem.data);
pAge.getValue(pAge.selectedItem.data);
pHeight.getValue(pHeight.selectedItem.data);
pWeight.getValue(pWeight.selectedItem.data);
inputStream = cr.openInputStream(Uri.parse(DataProvider.getItemAt(pGender:String, pDesire:String, pRace:String, pAge:String, pHeight:String, pWeight:String):Object));
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
//catch logic
} catch (IOException e) {
//catch logic
}
/**
* Clean up any open sockets when done
* transferring or if an exception occurred.
*/
finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
//catch logic
}
}
}
}