我有一个服务器正在运行,当我发送一个 httpget 时,我得到一个图像。我想在 android 中显示这张图片,所以我使用 asynctask 来下载文件。当我每次打开一个 tcp scoket 时它都有效,但是当我想在按下按钮时打开套接字并在按下另一个按钮时关闭它,而不是不断地制作新的套接字。
这是我使用的代码:
public class DroidCommV1Activity extends Activity {
Socket nsocket; //Network Socket
public Handler mHandler;
ImageView ivResponse;
Button get, stop;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
setContentView(R.layout.main);
get = (Button) findViewById(R.id.buttonGet);
ivResponse = (ImageView) findViewById(R.id.ivResponse);
stop = (Button) findViewById(R.id.buttonStop);
get.setOnClickListener(getlistener);
stop.setOnClickListener(stopListener);
}
private OnClickListener getlistener = new OnClickListener() {
public void onClick(View v) {
SocketAddress sockaddr = new InetSocketAddress("192.168.1.67", 8080);
nsocket = new Socket();
try {
nsocket.connect(sockaddr, 5000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //10 second connection timeout
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 1000);
}
};
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
new NetworkTask().execute();
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 2000);
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(mUpdateTimeTask);
try {
nsocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public class NetworkTask extends AsyncTask<Void, byte[], Integer> {
InputStream nis; //Network Input Stream
OutputStream nos; //Network Output Stream
Bitmap mp;
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Integer test) {
ivResponse.setImageBitmap(mp);
}
@Override
protected Integer doInBackground(Void... params) {
try {
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
nos.write("GET / HTTP/1.1\r\n\r\n".getBytes());
nos.flush();
mp = BitmapFactory.decodeStream(nis);
} catch (IOException e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: IOException");
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
} finally {
try {
nis.close();
nos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("AsyncTask", "doInBackground: Finished");
}
return 1;
}
}
我收到以下错误:
threadid=13: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: AsyncTask #4
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
at com.droidcomm.v1.DroidCommV1Activity$NetworkTask.doInBackground(DroidCommV1Activity.java:230)
at com.droidcomm.v1.DroidCommV1Activity$NetworkTask.doInBackground(DroidCommV1Activity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more