我的应用程序有问题,它在尝试向 Web 服务器发送一些数据时不断给我致命异常 AsyncTask #3。
如果我发送没有文件的数据,则会显示错误并终止应用程序,但如果我发送带有文件(jpg,mp4)的数据,一切都会很好。
这是日志:
10-25 00:31:59.274: E/AndroidRuntime(24895): FATAL EXCEPTION: AsyncTask #3
10-25 00:31:59.274: E/AndroidRuntime(24895): java.lang.RuntimeException: An error occured while executing doInBackground()
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.lang.Thread.run(Thread.java:856)
10-25 00:31:59.274: E/AndroidRuntime(24895): Caused by: java.lang.NullPointerException
10-25 00:31:59.274: E/AndroidRuntime(24895): at com.videotrafico.ReporteActivity$asyncreport.doInBackground(ReporteActivity.java:260)
10-25 00:31:59.274: E/AndroidRuntime(24895): at com.videotrafico.ReporteActivity$asyncreport.doInBackground(ReporteActivity.java:1)
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-25 00:31:59.274: E/AndroidRuntime(24895): ... 5 more
它给出错误的行是这一行:
file = new File(fileUri.getPath());
如果文件未设置,我如何覆盖应用程序而死?
谢谢!
异步任务:
class asyncreport extends AsyncTask< String, String, String > {
String rep;
String user;
protected void onPreExecute() {
//para el progress dialog
pDialog = new ProgressDialog(ReporteActivity.this);
pDialog.setMessage("Enviando reporte");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... params) {
//obtnemos usr y pass
rep=params[0];
user=params[1];
File file = new File(fileUri.getPath());
//enviamos y recibimos y analizamos los datos en segundo plano.
if (loginstatus(rep, user, file)==true){
return "ok"; //login valido
}else{
return "err"; //login invalido
}
}
/*Una vez terminado doInBackground segun lo que halla ocurrido
pasamos a la sig. activity
o mostramos error*/
protected void onPostExecute(String result) {
pDialog.dismiss();//ocultamos progess dialog.
Log.e("onPostExecute=",""+result);
if (result.equals("ok")){
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}else{
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
}
}
好的,问题public Uri fileUri;
是现在设置为public Uri fileUri = Uri.parse("");
但是该应用程序无论如何都不会发布数据!
这是帖子的代码:
try {
MultipartEntity entity = new MultipartEntity();
Log.e("enviando", fileUri.getPath());
entity.addPart("reporte", new StringBody(reporte));
entity.addPart("usuarioID", new StringBody(user));
entity.addPart("archivo", new FileBody(file));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}