2016-05-12 99 views
0

我正在調用我的活動中的Web服務。如果我連接到互聯網,它會調用Web服務,否則會從本地SqLite加載數據。 如果我連接到高速數據連接,它工作正常,但如果我通過慢連接連接它顯示等待對話框並不斷移動,最後我的應用程序崩潰並停止工作。如果應用連接到慢速連接,我想顯示一條正確的消息。無法檢測並處理網絡連接速度緩慢(WebServices)

我的代碼片段。

conn=new ConnectionDetector(getApplicationContext()); 
    isInternetPresent=conn.isConnectedToInternet(); 
    if(isInternetPresent) { 
     startWebServices(); 
    }else { 
     //load data in list view from SQLite 
    } 

// ----調用startWebServices()

public void startWebServices(){ 
     JSONReadText task=new JSONReadText(); 
     task.execute(new String[]{url}); 
    } 

// ----開始JSON READ DATA

public class JSONReadText extends AsyncTask<String,Void,String> { 

    public JSONReadText() { 
     super(); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MyRegionActivity.this); 
     pDialog.setTitle("Please Wait"); 
     pDialog.setMessage("Loading Data..."); 
     pDialog.setIndeterminate(true); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(params[0]); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("id", uid)); 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpClient.execute(httpPost); 
      JSONResult = inputStreamToString(response.getEntity().getContent()).toString(); 

     } catch (Exception e) { 
      MyRegionActivity.this.finish(); 

     } 
     return null; 
    } 

    private StringBuffer inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuffer answer = new StringBuffer(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     try { 
      while ((line = rd.readLine()) != null) { 
       answer.append(line); 
      } 
     } catch (Exception ex) { 
      MyRegionActivity.this.finish(); 
     } 
     return answer; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     ListDrawer(); 
     pDialog.dismiss(); 
    } 

} 

// ----的填充數據,以ListView

private void ListDrawer() { 
     regionList = new ArrayList<>(); 
     try { 
      JSONArray jsonMain= new JSONArray(JSONResult); 
      for (int i = 0; i < jsonMain.length(); i++) { 
       JSONObject jsonChild = jsonMain.getJSONObject(i); 
       String id=jsonChild.optString("id"); 
       String name=jsonChild.optString("name"); 
       String description=jsonChild.optString("description"); 
       String base_office=jsonChild.optString("base_office"); 
       regionList.add(new RegionList(id, name, description,base_office)); 
      } 
     } catch (Exception ee) { 
      MyRegionActivity.this.finish(); 
     } 
     ArrayAdapter adapter = new RegionListAdapter(MyRegionActivity.this, R.layout.activity_my_region, regionList); 
     adapter.notifyDataSetChanged(); 
     lv.setAdapter(adapter); 
+0

在這裏發佈logcat垃圾。並顯示完整的代碼。 – 2016-05-12 05:54:15

+0

LoginDataBaseAdapter類缺失?你有沒有製作適配器類? – Dhrupal

回答

1

您的項目是否有這個類「LoginDataBaseAdapter」。因爲它的紅色表示它的編譯錯誤。