我收到错误消息"执行doInBackground时发生错误"在我的http请求中添加连接超时和套接字超时时。像这样:
11-10 22:15:37.044: E/JavaBinder(8027): !!! FAILED BINDER TRANSACTION !!!
11-10 22:15:49.934: E/AndroidRuntime(8027): FATAL EXCEPTION: AsyncTask #3
11-10 22:15:49.934: E/AndroidRuntime(8027): Process: com.ad.tanker, PID: 8027
11-10 22:15:49.934: E/AndroidRuntime(8027): java.lang.RuntimeException: An error occured while executing doInBackground()
11-10 22:15:49.934: E/AndroidRuntime(8027): at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-10 22:15:49.934: E/AndroidRuntime(8027): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.lang.Thread.run(Thread.java:841)
11-10 22:15:49.934: E/AndroidRuntime(8027): Caused by: java.lang.NullPointerException: println needs a message
11-10 22:15:49.934: E/AndroidRuntime(8027): at android.util.Log.println_native(Native Method)
11-10 22:15:49.934: E/AndroidRuntime(8027): at android.util.Log.e(Log.java:232)
11-10 22:15:49.934: E/AndroidRuntime(8027): at com.ad.tanker.ViewFillOrder$ImageUploadTask.doInBackground(ViewFillOrder.java:1405)
11-10 22:15:49.934: E/AndroidRuntime(8027): at com.ad.tanker.ViewFillOrder$ImageUploadTask.doInBackground(ViewFillOrder.java:1)
11-10 22:15:49.934: E/AndroidRuntime(8027): at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-10 22:15:49.934: E/AndroidRuntime(8027): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-10 22:15:49.934: E/AndroidRuntime(8027): ... 4 more
这是我的代码
class ImageUploadTask extends AsyncTask <Void, Void, String>{
String err=null;
@Override
protected String doInBackground(Void... unsued) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var_date_confirm = sdf.format(new Date());
ByteArrayOutputStream bos_1 = new ByteArrayOutputStream();
ByteArrayOutputStream bos_2 = new ByteArrayOutputStream();
ByteArrayOutputStream bos_3 = new ByteArrayOutputStream();
bitmap_photo_1.compress(CompressFormat.JPEG, 100, bos_1);
bitmap_photo_2.compress(CompressFormat.JPEG, 100, bos_2);
bitmap_photo_3.compress(CompressFormat.JPEG, 100, bos_3);
byte[] data_1 = bos_1.toByteArray();
byte[] data_2 = bos_2.toByteArray();
byte[] data_3 = bos_3.toByteArray();
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost postRequest = new HttpPost(Variabel.URL_CONFIRM_ACTION);
ByteArrayBody bab_1 = new ByteArrayBody(data_1,var_photo_1);
ByteArrayBody bab_2 = new ByteArrayBody(data_2,var_photo_2);
ByteArrayBody bab_3 = new ByteArrayBody(data_3,var_photo_3);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(Variabel.KEY_ORDER_ID, new StringBody(var_order_id.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1, bab_1);
reqEntity.addPart(Variabel.KEY_PHOTO_1_TIME, new StringBody(var_photo_1_time.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1_LOCATION_LONGITUDE, new StringBody(var_photo_1_location_longitude.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1_LOCATION_LATITUDE, new StringBody(var_photo_1_location_latitude.toString().trim()));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
return s.toString().trim();
} catch (Exception e) {
err="error"+e.getMessage();
Log.e(e.getClass().getName(), e.getMessage());
return e.getMessage();
}
}
那么怎么解决呢?
对不起我的英文
答案 0 :(得分:0)
你应该在堆栈中寻找第一个例外
其实你的错误在这里:
err="error"+e.getMessage();
Log.e(e.getClass().getName(), e.getMessage());
e.getMessage()== null,你得到NullPointerException