Search code examples
androidweb-services3g

android application hangs when it connects to webservice using 3g connection and works fine with WIFI


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


Solution

  • 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