Doinbackground有时候永远不会完成对json的反应?

时间:2016-03-12 15:01:09

标签: android json android-studio android-asynctask

我的代码中存在问题。有时当我的互联网连接很差时,设备检测到我有互联网,所以当我打开活动时,我执行这个Asynctask:

class Something extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this){
            @Override
            public void onBackPressed() {
                pDialog.cancel();
                pDialog.dismiss();
                esCancelado=true;
                Intent intent = new Intent(MainActivity.this, PrincipalMenu.class);

                startActivity(intent);
                finish();
            }
        };
        pDialog.setMessage("Cargando. Por favor espere...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        // Obtenemos el string del JSON desde la URL
        JSONObject json = null;
        try {
            json = JSONParser.readJsonFromUrl(url);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        try {
            // Verifica que se cargaron datos
            int success;
            if(json == null){
                success = 0;
            }
            else {
                success = json.getInt(TAG_SUCCESS);
            }

            if (success == 1) {// Se encontraron complejos
                //do something
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                //do something
            }
        });
}

我认为问题出在这一行(只发生在我的大学里,我的网络连接很差,我没有电脑看到问题出在哪里):

json = JSONParser.readJsonFromUrl(url);

Doinbackground有时没有完成(因为对话框仍然加载并且从不解雇),如果我回去再次尝试活动我有同样的问题(无论我的互联网连接是否回去)所以我必须强制关闭应用程序并重新启动它,当我再次尝试该线程正常工作(如果我有一个良好的互联网连接)。我不知道是否有互联网缓存。

这是我的解析器类:

package com.sourcey.modeloUsuarios;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

public class JSONParser {

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try  {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        return new JSONObject(jsonText);
    } finally {
        is.close();
    }
}

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

}

谢谢你的帮助!谢谢!

0 个答案:

没有答案
相关问题