Java 尝试获取HttpURLConnection时,Android应用程序在后台没有响应

Java 尝试获取HttpURLConnection时,Android应用程序在后台没有响应,java,android,performance,android-asynctask,httpurlconnection,Java,Android,Performance,Android Asynctask,Httpurlconnection,我遇到了一个问题,当我尝试在我的应用程序中更新一些后台信息时,我经常收到ANR报告(没有响应) 代码块如下所示 class getServerTime extends AsyncTask<String, Long, Long> { private Long getInternetData() { String sURL = "http://crystalmathlabs.com/tracker/api.php?type=time"; //just a string

我遇到了一个问题,当我尝试在我的应用程序中更新一些后台信息时,我经常收到ANR报告(没有响应)

代码块如下所示

    class getServerTime extends AsyncTask<String, Long, Long> {
private Long getInternetData() {

    String sURL = "http://crystalmathlabs.com/tracker/api.php?type=time"; //just a string

    // Connect to the URL using java's native library
    HttpURLConnection connect = null;
    try {
        int responseCode = -1;

        List<String> listSet = new ArrayList();
        URL url = new URL(sURL);
        connect = (HttpURLConnection) url.openConnection();
        connect.setConnectTimeout(R.integer.timeoutLengthWithACapitalT);
        connect.setReadTimeout(R.integer.timeoutLengthWithACapitalT);
        connect.connect();  //this is the timeout line
        responseCode = connect.getResponseCode();
        if (responseCode < 400) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                listSet.add(inputLine);
            in.close();
            if (listSet.get(0) != null)
                return Long.parseLong(listSet.get(0));
        }
        return -1L;
    }
    catch (java.io.FileNotFoundException e) {
        Log.e("getServerTime", "File Not Found: " + e.getMessage());
    } catch (Exception e) {
        Log.e("getServerTime", "Unknown Exception " + e.getMessage());
    }
    finally{
        if (connect != null)
            connect.disconnect();
    }

    return -1L;
}

@Override
protected Long doInBackground(String[] params) {
    Long response;
    new Scores();
    try {
        response = getInternetData();
        if (response != null) {
                return response;
        }
        return -1L;
    } catch (Exception e) {
        Log.d("Error in getServerTime", "" + Log.getStackTraceString(e.getCause().getCause()));
    }

    return -1L;
}
类getServerTime扩展异步任务{ 私有Long getInternetData(){ 字符串sURL=”http://crystalmathlabs.com/tracker/api.php?type=time“;//只是一个字符串 //使用java的本机库连接到URL HttpURLConnection connect=null; 试一试{ int-responseCode=-1; List listSet=new ArrayList(); URL=新URL(sURL); connect=(HttpURLConnection)url.openConnection(); connect.setConnectTimeout(R.integer.timeoutLengthWithACapitalT); connect.setReadTimeout(R.integer.timeoutLengthWithACapitalT); connect.connect();//这是超时行 responseCode=connect.getResponseCode(); 如果(响应代码<400){ BufferedReader in=新的BufferedReader(新的InputStreamReader(connect.getInputStream()); 字符串输入线; 而((inputLine=in.readLine())!=null) 添加(inputLine); in.close(); if(listSet.get(0)!=null) 返回Long.parseLong(listSet.get(0)); } 返回-1L; } catch(java.io.filenotfound异常){ Log.e(“getServerTime”,“未找到文件:”+e.getMessage()); }捕获(例外e){ Log.e(“getServerTime”,“未知异常”+e.getMessage()); } 最后{ if(connect!=null) 连接。断开(); } 返回-1L; } @凌驾 受保护的长doInBackground(字符串[]参数){ 长响应; 新分数(); 试一试{ response=getInternetData(); if(响应!=null){ 返回响应; } 返回-1L; }捕获(例外e){ Log.d(“getServerTime中的错误”,“”+Log.getStackTraceString(例如getCause().getCause()); } 返回-1L; } }

在connect.connect上,我收到超时

以下是ANR报告的两个例子,我简直搞不懂,请帮忙

在android.os.AsyncTask.get(AsyncTask.java:507)
在 com.yargonauts.burk.runescapemarketwatch.crystalMathLabs.a.a (crystalMathFunctions.java:30)

您正在调用
AsyncTask.get()
,这几乎否定了异步任务的使用,因为它会在异步任务完成之前阻塞线程。 此阻塞导致ANR,因为主线程在等待结果时被卡住


您需要重写
onPostExecute(Result)
方法并在那里执行后续工作。查看asynctask javadoc的
用法部分:

从异步任务调用异步任务是否安全?因为我阻塞线程的原因是因为我需要在继续之前得到结果。这仅仅意味着你正在尝试以同步方式使用异步技术。异步方法是定义当结果可用时(AsyncTask的onPostExecute
)要做什么,而不是直接检索结果。试图以同步的方式使用异步代码会让你过得很不愉快。你救了我的命!非常感谢,我一直在努力解决这个问题!