AsyncTask抛出java.lang.RuntimeException?

时间:2014-11-26 13:21:44

标签: android android-asynctask http-post

我正在尝试使用HttpPost向Web服务发送信息。为了在我的Activity中执行此操作,我创建了一个AsyncTask,在doInBackground中,我调用了HttpPost的方法。问题是抛出任何异常,我不知道如何解决异常。

我在这里尝试。

//activity
private void doInsert(final Usuario usuario){       
    //nome, email e senha chegam preenchidos
    String[] em = etEmail.getText().toString().split("@");
    if(emailIsExist){
        Toast.makeText(this, "Email já cadastrado", Toast.LENGTH_SHORT).show();
    }else{
        progress = new CustomProgressDialog().getCustomProgress(null, CadastrarView.this);
        progress.show();
        //new UsuarioInsert().execute("insert");
        new AsyncTask<String, Void, String>(){
            @Override
            protected String doInBackground(String... params) {
                new UsuarioDAO().insert(usuario, fotoPerfil);
                return "executed";
            }   

            @Override
            protected void onPostExecute(String result) {
                progress.dismiss();
            }
        }.execute("");

    }
}

HttpPost

public Boolean insert(Usuario u, String fotoPath){
    HttpClient httpClient = new DefaultHttpClient();        
    HttpPost httppost = new HttpPost(urlPost.toString());
    Log.i("URL POST: ", urlPost.toString());

    try {
        File img = new File(fotoPath, ConvertStringToMD5.getMD5(u.getEmail().split("@")[0]));
        httppost.addHeader("Authorization", "Basic " + BasicAuthenticationRest.getBasicAuthentication());
        MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        me.addPart("nome", new StringBody(u.getNome()));
        me.addPart("email", new StringBody(u.getEmail()));
        me.addPart("senha", new StringBody(ConvertStringToMD5.getMD5(u.getSenha())));
        me.addPart("device_tipo", new StringBody("android"));
        me.addPart("device", new StringBody(AndroidReturnId.getAndroidId()));
        me.addPart("uploadedfile", new FileBody(img, "image/png"));         
        httppost.setEntity(me);

        HttpResponse response = httpClient.execute(httppost);
        HttpEntity entity = response.getEntity();
        Log.i("RESPONSE: ", EntityUtils.toString(entity));
        if(entity != null){
                String js = EntityUtils.toString(entity);
                Log.i("JSON: ", js);
                JSONObject json = new JSONObject(js);
                if(json.getString("cod").equals("999")){
                    return true;
                }                
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    return false;
}

异常

11-26 11:29:26.880: E/AndroidRuntime(13709): FATAL EXCEPTION: AsyncTask #1
11-26 11:29:26.880: E/AndroidRuntime(13709): java.lang.RuntimeException: An error occured while executing doInBackground()
11-26 11:29:26.880: E/AndroidRuntime(13709):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.lang.Thread.run(Thread.java:841)
11-26 11:29:26.880: E/AndroidRuntime(13709): Caused by: java.lang.NullPointerException
11-26 11:29:26.880: E/AndroidRuntime(13709):    at br.com.package.myapp.dao.UsuarioDAO.insert(UsuarioDAO.java:59)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at br.com.package.myapp.act.CadastrarView$UsuarioInsert.doInBackground(CadastrarView.java:180)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at br.com.package.myapp.act.CadastrarView$UsuarioInsert.doInBackground(CadastrarView.java:1)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-26 11:29:26.880: E/AndroidRuntime(13709):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-26 11:29:26.880: E/AndroidRuntime(13709):    ... 4 more
11-26 11:29:36.465: I/Choreographer(13709): Skipped 563 frames!  The application may be doing too much work on its main thread.
11-26 11:29:37.380: E/WindowManager(13709): Activity br.com.package.myapp.act.CadastrarView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42fe1460 V.E..... R......D 0,0-684,192} that was originally added here
11-26 11:29:37.380: E/WindowManager(13709): android.view.WindowLeaked: Activity br.com.package.myapp.act.CadastrarView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42fe1460 V.E..... R......D 0,0-684,192} that was originally added here
11-26 11:29:37.380: E/WindowManager(13709):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
11-26 11:29:37.380: E/WindowManager(13709):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
11-26 11:29:37.380: E/WindowManager(13709):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
11-26 11:29:37.380: E/WindowManager(13709):     at android.app.Dialog.show(Dialog.java:287)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.act.CadastrarView.doInsert(CadastrarView.java:152)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.act.CadastrarView.access$3(CadastrarView.java:145)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.act.CadastrarView$1.emailIsExist(CadastrarView.java:135)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.dao.UsuarioDAO$1.onResponse(UsuarioDAO.java:144)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.dao.UsuarioDAO$1.onResponse(UsuarioDAO.java:1)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.cv.ApplicationController.deliverResponse(ApplicationController.java:120)
11-26 11:29:37.380: E/WindowManager(13709):     at br.com.package.myapp.cv.ApplicationController.deliverResponse(ApplicationController.java:1)
11-26 11:29:37.380: E/WindowManager(13709):     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
11-26 11:29:37.380: E/WindowManager(13709):     at android.os.Handler.handleCallback(Handler.java:730)
11-26 11:29:37.380: E/WindowManager(13709):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 11:29:37.380: E/WindowManager(13709):     at android.os.Looper.loop(Looper.java:176)
11-26 11:29:37.380: E/WindowManager(13709):     at android.app.ActivityThread.main(ActivityThread.java:5419)
11-26 11:29:37.380: E/WindowManager(13709):     at java.lang.reflect.Method.invokeNative(Native Method)
11-26 11:29:37.380: E/WindowManager(13709):     at java.lang.reflect.Method.invoke(Method.java:525)
11-26 11:29:37.380: E/WindowManager(13709):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-26 11:29:37.380: E/WindowManager(13709):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-26 11:29:37.380: E/WindowManager(13709):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我的问题是当我发送图片时,我需要添加你的名字和类型。

这是我的表现。

public Boolean insert(Usuario u, String fotoPath){      
        HttpClient httpClient = new DefaultHttpClient();        
        HttpPost httppost = new HttpPost(urlPost.toString());       

        try {
            File img = new File(fotoPath);

            httppost.addHeader("Authorization", "Basic " + BasicAuthenticationRest.getBasicAuthentication());
            MultipartEntityBuilder me = MultipartEntityBuilder.create();
            me.addTextBody("nome", u.getNome());
            me.addTextBody("email", u.getEmail());
            me.addTextBody("senha", ConvertStringToMD5.getMD5(u.getSenha()));
            me.addTextBody("device_tipo", "android");
            me.addTextBody("device", AndroidReturnId.getAndroidId());
            me.addBinaryBody("uploadedfile", img, ContentType.create("image/jpg"), u.getEmail() + ".jpg");
            httppost.setEntity(me.build());

            HttpResponse response = httpClient.execute(httppost);           
            HttpEntity entity = response.getEntity();
            if(entity != null){
                String js = EntityUtils.toString(entity);
                JSONObject json = new JSONObject(js);
                if(json.getString("cod").equals("999")){
                    return true;
                }                
            }           
        } catch (ClientProtocolException e) {
            Log.e("ERROR: ", e.getLocalizedMessage());
        } catch (IOException e) {
            Log.e("ERROR: ", e.getLocalizedMessage());
        } catch (JSONException e) {
            Log.e("ERROR: ", e.getLocalizedMessage());
        } 
        return false;       
    }