私はWebサーバーにデータを送信しようとします。
最初。HttpResponse
私はデータがデバイスに送信されているかどうかを知るためだけに使用し、それは機能します
そしてResponseHandler
、データが送信されているか、Webサーバーで読み取られていないかを知らせようとします。
私は2つの応答を作成します。
データが送信されないか失敗する
と、エラーメッセージとトーストが表示され、機能します
ただし、データが送信されるとトーストが表示されますが、エラーメッセージは表示されず、強制的に閉じられます
ここに私のコードがあります
private boolean SendDataFoto(CPetakRecord rec){
String slon=String.format("%f", rec.dLongitude);
String slat=String.format("%f", rec.dLatitude);
String sacc=String.format("%f", rec.dAccuracy);
String jnsHm=String.format(rec.jenisHm);
String jmlHm=String.format(rec.jumlahHm);
String jnsMsh=String.format(rec.jenisMsh);
String jmlMsh=String.format(rec.jumlahMsh);
String intens=String.format(rec.intensitas);
String luasAncam=String.format(rec.luasTerancam);
String nmPetak=String.format(rec.nmPetak);
String code=String.format(rec.code);
String userId=String.format(rec.userId);
String varietas=String.format(rec.varietas);
String dbLon=String.format("%f", rec.dbLon);
String dbLat=String.format("%f", rec.dbLat);
File file=new File(rec.sFilename);
String sfname=file.getName();
String cfname=file.getName();
String lfname=rec.sFilename;
sfname = sfname.substring(0, sfname.lastIndexOf("."));
Calendar cal=Calendar.getInstance();
cal.setTime(new Date(rec.lDate));
String sdat=CUtilities.DateToString(cal);
String url;
String spetak;
if (CGeneral.nPetakTetap==CGeneral.ACTIVITY_FOR_PETAK){
url = CGlobalConfig.getURLRcvrPetak();
spetak="petak";
}
else{
url = CGlobalConfig.getURLRcvrPeliharaImage();
spetak="pelihara";
}
Bitmap bmp=BitmapFactory.decodeFile(lfname);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
HttpEntity entity=null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(ba, spetak+"_"+cfname);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);
reqEntity.addPart("category", new StringBody(spetak));
reqEntity.addPart("foto", new StringBody(spetak+"_"+cfname));//
reqEntity.addPart("tgl", new StringBody(sdat));//
reqEntity.addPart("lat_petak", new StringBody(slat));//
reqEntity.addPart("lon_petak", new StringBody(slon));//
reqEntity.addPart("accuracy", new StringBody(sacc));
reqEntity.addPart("jenis_hama", new StringBody(jnsHm));//
reqEntity.addPart("jumlah_hama", new StringBody(jmlHm));//
reqEntity.addPart("jenis_musuh", new StringBody(jnsMsh));//
reqEntity.addPart("jumlah_musuh", new StringBody(jmlMsh));//
reqEntity.addPart("int_serangan", new StringBody(intens));//
reqEntity.addPart("luas_terancam", new StringBody(luasAncam));//
reqEntity.addPart("nama_petak", new StringBody(nmPetak));//
reqEntity.addPart("code_db", new StringBody(code));//
reqEntity.addPart("user_id", new StringBody(userId));//
reqEntity.addPart("varietas", new StringBody(varietas));//
reqEntity.addPart("lon_db", new StringBody(dbLon));//
reqEntity.addPart("lat_db", new StringBody(dbLat));//
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response2 = httpclient.execute(httppost, responseHandler);
//if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
final String reverseString = response2;
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UpdateLog.this, "response : " + reverseString, Toast.LENGTH_LONG).show();
}
});
entity = response.getEntity();
//}
}
catch (final ClientProtocolException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UpdateLog.this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
}
});
// TODO Auto-generated catch block
} catch (final IOException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UpdateLog.this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
}
});
// TODO Auto-generated catch block
}
catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
String temp = null;
try {
if(entity == null){
publishProgress(0,"Send Fail");
SystemClock.sleep(500);
return false;
}
else{
temp = EntityUtils.toString(entity);
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (temp.compareTo("SUCCESS") == 0) {
publishProgress(0,"Send Ok");
SystemClock.sleep(500);
return true;
}
else{
publishProgress(0,"Send Fail");
return false;
}
}
ここに私のlogcatがあります
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): FATAL EXCEPTION: AsyncTask #2
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): java.lang.RuntimeException: An error occured while executing doInBackground()
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.lang.Thread.run(Thread.java:1019)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): Caused by: java.lang.NullPointerException
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at hariff.ltis.petaktetap.UpdateLog$MyAsyncTaskPetak.SendDataFoto(UpdateLog.java:961)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at hariff.ltis.petaktetap.UpdateLog$MyAsyncTaskPetak.doInBackground(UpdateLog.java:814)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at hariff.ltis.petaktetap.UpdateLog$MyAsyncTaskPetak.doInBackground(UpdateLog.java:1)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-28 00:38:43.421: ERROR/AndroidRuntime(3028): ... 4 more
エラーは並んでいますif (temp.compareTo("SUCCESS") == 0) {
それを修正するにはどうすればいいですか? データが送信されると、トーストとエラーメッセージが一緒に表示されます。