サーバーを実行していて、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