3

我想在我的 android 应用程序中使用 .net 网络服务。我使用 asynctask 向 Web 服务发送请求。但是请求没有正确地取值。我在许多 android 应用程序中使用了这种结构,但我从来没有遇到过这样的错误。问题是什么?

我有权限(互联网)。命名空间和方法名称为真。

    public class MainActivity extends Activity {

        private static final String NAMESPACE = "http://tempuri.org/";  
        private static final String URL = "http://service.melihmucuk.com/ShopArWS.asmx";
        private String[][] items;

    private String[][] GetAllItems(){

        SoapObject request = new SoapObject(NAMESPACE, "GetAllItem");//faulty line
            SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;     
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;

           try {

           androidHttpTransport.call("http://tempuri.org/GetAllItem", envelope);
           SoapObject response = (SoapObject) envelope.getResponse();
           items[0] = new String[response.getPropertyCount()]; //item_id
           items[1] = new String[response.getPropertyCount()]; //price
           items[2] = new String[response.getPropertyCount()]; //title
           items[3] = new String[response.getPropertyCount()]; //desc

           for(int i=0;i<response.getPropertyCount();i++){    
                   Object property = response.getProperty(i);
                   if(property instanceof SoapObject){
                       SoapObject item = (SoapObject) property;
                       String item_id = item.getProperty("item_id").toString();
                       String price = item.getProperty("price").toString();
                       String title = item.getProperty("title").toString();
                       String desc = item.getProperty("desc").toString();

                       items[0][i] = item_id;
                       items[1][i] = price;
                       items[2][i] = title;
                       items[3][i] = desc;
                   }    
           }
        }
            catch (Exception e) {           
                e.printStackTrace();
           }   
           return items;
        }

    public class GetAllItemsAS extends AsyncTask<String,String,String[][]>{



        @Override
         protected void onPreExecute() {

         }

    @Override
    protected String[][] doInBackground(String... params) {
        GetAllItems();
        return items;
    }

    protected void onPostExecute(String[][] items){
        ASFinish();

    }
}

    public void ASFinish(){
        ListView liste = (ListView)findViewById(R.id.listView1);
        ListArrayAdapter adapter = new ListArrayAdapter(MainActivity.this,items[0],items[1],items[2],items[3]);
        liste.setAdapter(adapter);
    }



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            items = new String[4][0];
                    new GetAllItemsAS().execute();

        }
    }

我的日志猫

05-22 01:07:10.201: E/WindowManager(22337): Activity com.zontul.shopar.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@414ba3a0 that was originally added here
05-22 01:07:10.201: E/WindowManager(22337): android.view.WindowLeaked: Activity com.zontul.shopar.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@414ba3a0 that was originally added here
05-22 01:07:10.201: E/WindowManager(22337):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:464)
05-22 01:07:10.201: E/WindowManager(22337):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
05-22 01:07:10.201: E/WindowManager(22337):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
05-22 01:07:10.201: E/WindowManager(22337):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
05-22 01:07:10.201: E/WindowManager(22337):     at android.view.Window$LocalWindowManager.addView(Window.java:558)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.Dialog.show(Dialog.java:282)
05-22 01:07:10.201: E/WindowManager(22337):     at com.zontul.shopar.MainActivity$GetAllItemsAS.onPreExecute(MainActivity.java:78)
05-22 01:07:10.201: E/WindowManager(22337):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-22 01:07:10.201: E/WindowManager(22337):     at android.os.AsyncTask.execute(AsyncTask.java:534)
05-22 01:07:10.201: E/WindowManager(22337):     at com.zontul.shopar.MainActivity.onCreate(MainActivity.java:115)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.Activity.performCreate(Activity.java:5066)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
05-22 01:07:10.201: E/WindowManager(22337):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 01:07:10.201: E/WindowManager(22337):     at android.os.Looper.loop(Looper.java:155)
05-22 01:07:10.201: E/WindowManager(22337):     at android.app.ActivityThread.main(ActivityThread.java:5520)
05-22 01:07:10.201: E/WindowManager(22337):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 01:07:10.201: E/WindowManager(22337):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 01:07:10.201: E/WindowManager(22337):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
05-22 01:07:10.201: E/WindowManager(22337):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
05-22 01:07:10.201: E/WindowManager(22337):     at dalvik.system.NativeStart.main(Native Method)

编辑:我编辑了我的代码,我不使用对话框。请不要写关于对话的答案。对话不会出现问题。

4

3 回答 3

1

您将需要在ProgressDialog内部onPreExecute而不是在类级别进行初始化:

private ProgressDialog dialog;  //declare here

    @Override
     protected void onPreExecute() {
        dialog = new ProgressDialog(MainActivity.this); //<< initialize here
        dialog.setMessage("Loading...");
        dialog.show();
     }
 //.....
protected void onPostExecute(String[][] items){
    ASFinish();
    if(null !=dialog)
     if(dialog.isShowing())
       dialog.dismiss();
}
于 2013-05-21T22:32:29.527 回答
1

该错误是因为您有另一个/上一个错误,AsynTask并且dialog 绑定到此活动的绑定尚未解除,它会激活您发布的异常。

顺便说一句,如果您想停止AsynTask活动是否被破坏,请记住

  • 在 处取消异步任务onDestroy
  • 如果AsynTask取消,则覆盖该onCancelled功能并检查条件以doinBackground尽快完成任务。
  • 同时onCancelled关闭对话框。

否则,您的活动可能会在 onPostExecute 之前被销毁,并且将在该函数上激活异常。

可以通过调用 cancel(boolean) 随时取消任务。调用此方法将导致对 isCancelled() 的后续调用返回 true。调用此方法后,将在 doInBackground(Object[]) 返回后调用 onCancelled(Object),而不是 onPostExecute(Object)。为了确保尽快取消任务,如果可能(例如在循环内),您应该始终从 doInBackground(Object[]) 定期检查 isCancelled() 的返回值。

参考:AsynTask API

于 2013-05-24T09:27:43.397 回答
1

实际问题出在 GetAllItems() 方法中,它正在生成异常,说明活动将要完成的原因,然后您尝试显示进度对话框做一件事,将 try catch 放入 GetAllItems() 并尝试在 doInBackground 中运行此代码。

于 2013-05-28T07:30:14.200 回答