0

I am developing an android application that consumes web service , the service output is XML

I am connecting to the web service using this code

public  String converse(String host, int port, String path)
            throws IOException, URISyntaxException {
    
        BufferedReader in = null;
        String serviceResponse = null ;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            String serviceUrl = "http://"+host+":"+port+path;
            System.out.println("service url "+serviceUrl);
            request.setURI(new URI(serviceUrl));
            
            System.out.println("Request "+request.toString());
            HttpResponse response = client.execute(request);
            
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            serviceResponse  = sb.toString();
            
            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return serviceResponse;
    }

when the application launches with WI-FI every thing works fine and when I restart the application with 3G connection it hangs and displays a the following dialog waiting dialog in android

In addition to this code I am using this method inside another method

public void fillAdapter() {
        //calling web service using the mentioned method
    }

And this function used inside an async task to fill ListView adapter

protected class AsyncLoading extends AsyncTask<Void,Void, BaseModel[]>{
         
        @Override 
        protected void onPreExecute(){
            pd =ProgressDialog.show(BaseListActivity.this, "loading in progress", "waiting .");
        }
        @Override
        protected BaseModel[] doInBackground(Void... params) {
            fillAdapter();
            return listItems;
    
        }
        @Override
        protected void onPostExecute(BaseModel[] doc){
            //list.setAdapter(doc);
            if(doc != null) {
            if(doc.length > 0 ) {
                if(doc[0] instanceof Activity)
                    adapter = new OffersListAdapter(getApplicationContext(),doc);
                else if (doc[0] instanceof Offer)
                    adapter = new OffersListAdapter(getApplicationContext(),doc);
                else if (doc[0] instanceof Branch) {
                    adapter = new BranchListAdapter(getApplicationContext(),doc);
                    Log.i("Branch"," Added Branch");
                }else if (doc[0] instanceof Consolation) {
                    adapter = new ListAdapter(getApplicationContext(),doc);
                    adapter.setDisplayImage(false);
                }else if ( doc[0] instanceof Event) {
                    adapter = new EventListAdapter(getApplicationContext(),doc);
                    
                }
                else
                    adapter = new ListAdapter(getApplicationContext(),doc);
                
            }//end if doc != null
                list.setAdapter(adapter);
                
            }
            handler.sendEmptyMessage(0);
        }

I saw this post but I don't have a good result I'm working on this problem for 2 days with my thanks in advance .

Note : this problem often appears the first time the application connects to the service after that if I pressed wait and the application continued then al other activities consuming the web service will work fine

Community
  • 1
  • 1
Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
  • Whatever you do, be sure to patch 3G edge kind of networks that uses gsm operator service for connection failed during incoming calls. As they are able to work one at a time. – Gökhan Barış Aker Jun 12 '12 at 14:08

4 Answers4

2

I think you have low speed connection of 3G compared to Wi-Fi.So Use Asynctask to load data from server in seperate thread rather than main thread.It is good idea to show ProgressDialog while fetching the data.

And In some cases Apis will work in Wi-Fi and may not in 3G connection.So test Your url in Device browser also to make it confirm

Community
  • 1
  • 1
Abhi
  • 8,935
  • 7
  • 37
  • 60
  • actually I am using this function inside an async task , and I show a progressbar .additionally I test the url in built-in browser and it works fine with both 3g and wifi . my 3g connection has more speed than wifi – Muhannad A.Alhariri Jun 12 '12 at 14:33
  • No clearly it is saying you are doing the stuff in Main thread.., Do in seperate thread – Abhi Jun 12 '12 at 14:37
  • in the main thread I am usind asyntask.execte() as I updated above – Muhannad A.Alhariri Jun 12 '12 at 14:43
  • Yes dude your code looking good.., any way tomorrow we will discuss [here](http://chat.stackoverflow.com/rooms/5098/android-people).., Now leaving – Abhi Jun 12 '12 at 14:50
0

You should use an AsyncTask for every task that could take some time to be executed. See http://developer.android.com/reference/android/os/AsyncTask.html

Alexandre B.
  • 495
  • 1
  • 4
  • 16
0

The problem i am seeing with your code is you are not doing network operation in Thread. So if you perform asynchronous operation on Main thread,application will display above dialog if it don't receive response in 5 seconds.In your case 3g connection may be taking more than 5 seconds to return response.

Best bet is include above code in Thread!!

Vipul
  • 27,808
  • 7
  • 60
  • 75
0

I have solved the problem , that I have a splash screen activity inside which I use C2DM and register the device in that activity using service

            registrationIntent.putExtra("app", PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(), 0));

        registrationIntent.putExtra("sender", "someemailaddress@gmail.com");
        startService(registrationIntent);

I put this code inside an asyncTask and didn't block the UI Thanks for every one who tried to help me

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46