我尝试将数据发送到网络服务器。
第一。我只使用HttpResponse
来了解数据是否已在设备中发送并且可以正常工作
我尝试让ResponseHandler
知道在网络服务器中发送或未读取数据。
我创建两个回复。
当数据未发送或失败时,
他们两个,错误信息和吐司是显示和工作
但是当发送数据时,显示toast但错误信息未显示,然后强制关闭
这是我的代码
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) {
如何解决这个问题?我希望在发送数据时一起显示toast和错误消息。
答案 0 :(得分:0)
在这里:
if(entity == null){
publishProgress(0,"Send Fail");
SystemClock.sleep(500);
return false;
}
似乎抛出异常会阻止代码运行return false
。捕获到异常,但temp
仍未初始化。所以你看到几行NullPointerException。
你应该看到初始错误的橙色堆栈跟踪,解决那里的错误,这个NullPointerException将不再是一个问题。 (您可能还想将return false;
添加到catch块中。
最后我假设SystemClock.sleep(500);
是问题,阻塞任何线程是不必要的,你应该使用一个处理程序和Runnable来延迟后运行代码。