簡體   English   中英

android應用程序在使用3g連接連接到webservice時掛起,並且可以正常使用WIFI

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

我正在開發一個使用Web服務的android應用程序,服務輸出是XML

我正在使用此代碼連接到Web服務

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;
    }

當應用程序啟動WI-FI時,每件事都運行正常,當我用3G連接重新啟動應用程序時,它會掛起並顯示以下對話框 在android中等待對話框

除了這段代碼,我在另一個方法中使用此方法

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

並且此函數在異步任務中用於填充ListView適配器

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

我看到了這篇文章,但是我沒有取得好成績,我提前2周感謝這個問題。

注意:此問題經常出現在應用程序第一次連接到服務之后,如果我按下等待並且應用程序繼續,那么使用Web服務的其他活動將正常工作

我認為與Wi- Fi相比,你有3G的低速連接。所以使用Asynctask在單獨的線程而不是主線程中從服務器加載數據。在獲取數據時顯示ProgressDialog是個好主意。

在某些情況下,Apis可以在Wi-Fi中工作,也可能不在3G連接中。所以在設備瀏覽器中測試你的網址也要確認

您應該為可能需要一些時間才能執行的每個任務使用AsyncTask。 請參閱http://developer.android.com/reference/android/os/AsyncTask.html

我看到你的代碼的問題是你沒有在線程中進行網絡操作。 因此,如果您在主線程上執行異步操作,如果在5秒內沒有收到響應,應用程序將顯示在上面的對話框中。在您的情況下,3g連接可能需要5秒以上才能返回響應。

最好的選擇是在Thread中包含上面的代碼!!

我已經解決了這個問題,我有一個啟動畫面活動,我在其中使用C2DM並使用服務在該活動中注冊設備

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

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

我將此代碼放在asyncTask中並且沒有阻止UI感謝每個試圖幫助我的人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM