3

I want to use .net web service in my android app. I send request to web service by using asynctask. But request doesn't take value properly. I used this structure in many android apps and I never got error like this. What is the problem?

I have permission(INTERNET). Namespace and method name are true.

    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();

        }
    }

My LogCat

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)

Edit: I edited my code and I don't use dialog. Please don't write an answer about dialog. Problem doesn't occur by dialog.

Melih Mucuk
  • 6,988
  • 6
  • 37
  • 56

3 Answers3

1

you will need to initialize ProgressDialog inside onPreExecute instead of at class level:

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();
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

The error is because there was another/previous error on your AsynTask and the dialog binded to this activity has not been dismissed, it activates the exception you posted.

By the way if you want to stop the AsynTask if the activity is destroyed, remmember to

  • Cancel the asyntask at onDestroy.
  • If the AsynTask is cancelled override the onCancelled function and check the condition on the doinBackground to finish the task as soon as possible.
  • At onCancelled also dismiss the dialog.

Otherwise your activity can be destroyed before onPostExecute and an exception will be activated on that function.

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Reference: AsynTask API

AlexBcn
  • 2,450
  • 2
  • 17
  • 28
1

Actual Problem is in the GetAllItems() method it is generating the exception that why the activity is going to finish and after that you are trying to show the progress dialog box do one thing that put the try catch in the GetAllItems() and try to run this code in doInBackground.

UnderGround
  • 450
  • 1
  • 3
  • 17