-1

我的应用程序有问题,它在尝试向 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();
                }
4

3 回答 3

1

你还没有初始化fileUri一个值,你可以通过使用构造函数将 Uri 传递给 AsyncTask 来做到这一点。

      public class asyncReport extends AsyncTask<String,String,String>
      {
          String user,rep
          String fileUri;

          public asyncReport(String fileUri)
          {
             this.fileUri=fileUri;
          }

         ...
      } 

此外,当您说<input,progress,output>必须<String,String,String>使用字符串发布操作进度时。我建议您改用<String,Integer,String>。为了发布进度,请使用:

      //In your doInBackground
        publishProgress(progress);//create and maintain a variable to do this

      //in your AsyncTask
      onProgressUpdate(int progress)       
      {
         setProgressPrecent(progress);
      }   

您还可以将 fileUri 作为参数之一传递给您的 doInBackground。

于 2013-10-25T05:48:47.563 回答
1

您可以简单地进行空检查并继续执行您的代码,我想应该这样做。

if(fileUri!=null)
{
     file = new File(fileUri.getPath());
     // do all relevant part here. 
}
于 2013-10-25T05:12:03.283 回答
-2

我怀疑这就是您要调用异步任务的方式:

HttpTraitement task = new HttpTraitement(this);
task.execute("http://namedomain/test.php");
In order to do that, you must change your async task the following way:

public class HttpTraitement extends AsyncTask<String, Integer, HttpResponse> {

    private Context context;

    public HttpTraitement (Context context) 
    {
        this.context = context;     
    }

    @Override
    protected HttpResponse doInBackground(String... params) {
        HttpResponse response = null;
        HttpPost httppost = new HttpPost(params[0]);

        try {
            HttpClient client = new DefaultHttpClient();
            response = client.execute(httppost); 

        } catch (ClientProtocolException e) {
            Toast.makeText(this.context, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this.context, "Caught IOException", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this.context, "Exception", Toast.LENGTH_SHORT).show();                  
        }

        return response;

    }

}
Please note that the method execute() must be called only once for each async task object. Thus if you want to call it for more than one URL, you must do this:

HttpTraitement task1 = new HttpTraitement(this);
task1.execute("http://namedomain/test1.php");
HttpTraitement task2 = new HttpTraitement(this);
task2.execute("http://namedomain/test2.php");
于 2013-10-25T05:13:55.777 回答