我需要通过 WI-FI Direct 传输一个 txt 文件。问题是应用程序在传输开始后一直在崩溃(在客户端(那个,那个传输))。Wi-Fi Direct 没有问题,因为我可以毫无问题地发送字符串等。我相信,这个问题与读取文件有关。文件在空 SD 卡上。这里只有 LOST.DIR + Alice_commodities.txt(我需要传输的文件)。
这是传输类
private class Networking_files_transmit extends AsyncTask<String, Void, Void>
{
public final int port=8888;
@Override
protected Void doInBackground(String... params) {
String ServerIP=params[0];
Socket s=null;
OutputStream out=null;
Context context = null;
BufferedInputStream in=null;
FileInputStream fis=null;
try {
s=new Socket(ServerIP,port);
out= s.getOutputStream();
} catch (UnknownHostException e) {
Log.e("Files transmission", "Can not find host");
e.printStackTrace();
} catch (IOException e) {
Log.e("Files transmission", "Problems with IO");
e.printStackTrace();
}
if(s.isConnected())
{
int count;
byte[] buffer=new byte [1024];
final File myFile=new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Alice_commodities.txt");
try {
in=new BufferedInputStream( new FileInputStream(myFile));
while((count=in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
} catch (FileNotFoundException e) {
Log.e("Files transmission", "Can not find file");
e.printStackTrace();
} catch (IOException e) {
Log.e("Files transmission", "Problems with IO, something wrong with file");
e.printStackTrace();
}
try {
out.close();
in.close();
s.close();
} catch (IOException e) {
Log.e("Files transmission", "Cannot close stream or socket");
e.printStackTrace();
}
}
else
{
Log.e("Files transmission", "Socket could not connect");
try {
out.close();
in.close();
s.close();
} catch (IOException e) {
Log.e("Files transmission", "Cannot close stream or socket");
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast toast=Toast.makeText(getApplicationContext(), "File transmited", Toast.LENGTH_SHORT);
toast.show();
}
}
和接收类
private class Networking_input_server_file_receive extends AsyncTask<Void, Void, Void>
{
public final int port=8888;
public long start;
public long end;
@Override
protected Void doInBackground(Void... params) {
ServerSocket ss= null;
Socket incoming=null;
FileOutputStream fos=null;
BufferedOutputStream out=null;
InputStream in=null;
try {
fos = openFileOutput("new.txt", Context.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
Log.e("FileReceiving", "Could not find a file");
e1.printStackTrace();
}
try {
ss=new ServerSocket(port);
ss.setSoTimeout(20000);
incoming= ss.accept();
out = new BufferedOutputStream(fos);
} catch (UnknownHostException e) {
Log.e("FileReceiving", "Could not find a host");
e.printStackTrace();
} catch (IOException e) {
Log.e("FileReceiving", "Problems with IO");
e.printStackTrace();
}
if (incoming.isConnected())
{
start=System.currentTimeMillis();
byte[] buffer = new byte[1024];
int count;
try {
in = incoming.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while((count=in.read(buffer)) > 0){
fos.write(buffer, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
end=System.currentTimeMillis();
fos.close();
incoming.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("file receiving", "Connection failed");
try {
fos.close();
incoming.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String s=String.valueOf(end-start);
Toast toast=Toast.makeText(getApplicationContext(), s , Toast.LENGTH_SHORT);
toast.show();
}
}
和清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.wifidirect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.paad.wifidirect.WiFiDirectActivity"
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>
提前感谢您的帮助。
来自变送器的UPD错误日志
07-09 10:43:32.653 E/AndroidRuntime(12614): FATAL EXCEPTION: AsyncTask #1
07-09 10:43:32.653 E/AndroidRuntime(12614): java.lang.RuntimeException: An error occured while executing doInBackground()
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.lang.Thread.run(Thread.java:856)
07-09 10:43:32.653 E/AndroidRuntime(12614): Caused by: java.lang.NullPointerException
07-09 10:43:32.653 E/AndroidRuntime(12614): at com.paad.wifidirect.WiFiDirectActivity$Networking_files_transmit.doInBackground(WiFiDirectActivity.java:524)
07-09 10:43:32.653 E/AndroidRuntime(12614): at com.paad.wifidirect.WiFiDirectActivity$Networking_files_transmit.doInBackground(WiFiDirectActivity.java:1)
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-09 10:43:32.653 E/AndroidRuntime(12614): ... 5 more