无法检测和处理慢速网络连接。(WebServices)

时间:2016-05-12 05:51:17

标签: android sdk http-post

我在我的活动中呼叫网络服务。如果我连接到Internet,则调用Web服务,否则从本地SqLite加载数据。 如果我连接到高速数据连接,它的工作正常但是,如果我通过慢速连接连接它会显示等待对话框并持续移动,最后我的应用崩溃并停止工作。如果App连接到慢速连接,我想显示正确的消息。

我的代码剪辑。

XY

// ----调用startWebServices()

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

// ----启动JSON READ DATA

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

// ----将数据填充到ListView

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

}

1 个答案:

答案 0 :(得分:1)

您的项目是否具有此类&#34; LoginDataBaseAdapter&#34;。因为它的红色表示其编译错误。