Завершить диалог прогресса с обработчиком - PullRequest
0 голосов
/ 11 августа 2011

Мне нужно знать, как закрыть диалог прогресса, когда готов http-пост.

Это то, что я имею до сих пор:

httppost.setEntity (мне);

 HttpResponse responsePOST = client.execute(httppost);  
 HttpEntity resEntity = responsePOST.getEntity(); 

 InputStream inputstream = resEntity.getContent();
 BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));
 StringBuilder stringbuilder = new StringBuilder();
 String currentline = null; 

 while ((currentline = buffered.readLine()) != null)
    { 
     stringbuilder.append(currentline + "\n"); 
     String result = stringbuilder.toString(); 
     Log.v("HTTP UPLOAD REQUEST",result); 
     inputstream.close();  
     handler.sendMessage(handler.obtainMessage());}
      }
     catch (Exception e) {
     e.printStackTrace();
     Toast.makeText(share.this, "Upload failed", Toast.LENGTH_SHORT).show();
    }
    }

    });

    background.start();
    }

    Handler handler = new Handler (){
    public void HandleMessage (Message msg){
    dialog.dismiss();
    }
    };

Это не работает, что я должен положить в обработчик отправляющего сообщения, чтобы сообщить обработчику об отклонении? Заранее спасибо

Janail после исправления выдает мне следующие ошибки:

08-15 10:08:20.020: ERROR/AndroidRuntime(25690): FATAL EXCEPTION: Thread-10
08-15 10:08:20.020: ERROR/AndroidRuntime(25690): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.os.Handler.<init>(Handler.java:121)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.widget.Toast.<init>(Toast.java:68)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.widget.Toast.makeText(Toast.java:231)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at com.apapa.vrsixty.share$2$2.run(share.java:130)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at java.lang.Thread.run(Thread.java:1027)
08-15 10:08:20.781: ERROR/WindowManager(25690): Activity com.apapa.vrsixty.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054d2f8 that was originally added here
08-15 10:08:20.781: ERROR/WindowManager(25690): android.view.WindowLeaked: Activity com.apapa.vrsixty.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054d2f8 that was originally added here
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.ViewRoot.<init>(ViewRoot.java:277)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.Dialog.show(Dialog.java:265)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.apapa.vrsixty.share$2.onClick(share.java:91)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.View.performClick(View.java:2532)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.View$PerformClick.run(View.java:9293)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Handler.handleCallback(Handler.java:587)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Looper.loop(Looper.java:143)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ActivityThread.main(ActivityThread.java:4263)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at java.lang.reflect.Method.invoke(Method.java:507)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at dalvik.system.NativeStart.main(Native Method)

1 Ответ

0 голосов
/ 12 августа 2011

Попробуйте вот так

HttpResponse responsePOST = client.execute(httppost);  
 HttpEntity resEntity = responsePOST.getEntity(); 

 InputStream inputstream = resEntity.getContent();
 BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));
 StringBuilder stringbuilder = new StringBuilder();
 String currentline = null; 

 while ((currentline = buffered.readLine()) != null)
    { 
     stringbuilder.append(currentline + "\n"); 
     String result = stringbuilder.toString(); 
     Log.v("HTTP UPLOAD REQUEST",result); 
     inputstream.close();  
     //handler.sendMessage(handler.obtainMessage());}
      }
     catch (Exception e) {
     e.printStackTrace();
     Toast.makeText(share.this, "Upload failed", Toast.LENGTH_SHORT).show();
    }
     Message msg = Message.obtainMessage();
     msg.arg1 = 1;
     handler.sendMessage(msg);

    }

    });

    background.start();
    }

    private final Handler handler = new Handler (){
    public void HandleMessage (Message msg){
         if(msg.arg1 == 1)
               dialog.dismiss();
    }
    };
...