Asynctask挂在getInputStream()

时间:2019-01-21 19:58:26

标签: java android android-asynctask java.util.scanner

我已经在Android应用程序的这一部分工作了几个小时,并且我已经阅读了至少50篇相关的文章,但是都没有帮助。我有一个AsyncTask,试图从网站上获取文本。这应该非常简单。

我已经有一个Java Swing程序,该程序可以调用非常相似的代码,这些代码可以按照我想要的方式正常工作,因此我知道网站和Internet已启动并正在运行。

当我尝试运行该应用程序时,AsyncTask挂在注释点,我不知道为什么。

AsyncTask内部版本的简化版本:

@Override
protected void onPreExecute() {
    Log.wtf("PROGRESS", "in preex");
    //progress bar
}

@Override
protected String doInBackground(Void... params) {
    android.os.Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE);

    try {
        Log.wtf("PROGRESS", "in try");
        URLConnection connection = new URL("https://bmorgan17.github.io/html-database/").openConnection();
        Log.wtf("PROGRESS", "connect");
        Scanner scanner = new Scanner(connection.getInputStream()); //HANGS HERE
        Log.wtf("PROGRESS", "got stream");
        scanner.useDelimiter("\\Z");
        string = scanner.next();
        Log.wtf("PROGRESS", "read");
        scanner.close();
    } catch ( Exception ex ) {
        ex.printStackTrace();
        Log.wtf("FAILED", "Hard rip mate");
    }

    return string;
}

@Override
protected void onPostExecute(String s) {
    Log.wtf("PROGRESS", "in post");
    //hide progress bar
    delegate.processFinish(s);
}

Logcat:

01-21 13:36:53.039 14480-14480/com.example.name.stemapp A/PROGRESS: in preex
01-21 13:36:53.109 14480-14495/com.example.name.stemapp A/PROGRESS: in try
01-21 13:36:53.125 14480-14495/com.example.name.stemapp A/PROGRESS: connect

在最后输入logcat后,进度轮将永远旋转,任何帮助将不胜感激。

编辑:

在创建此帖子和Reddit冲浪15-20分钟所花费的时间中,该应用终于得以继续。它进入catch语句,然后执行postExecute,这显然不是我想要的。如果有人可以帮助我解决这个新问题,那就太好了。

Logcat:

A/PROGRESS: in preex
A/PROGRESS: in try
A/PROGRESS: connect
A/FAILED: hard rip mate
A/PROGRESS: in post

2 个答案:

答案 0 :(得分:0)

首先,在 AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

尝试以下方法:

    URL url;
    HttpURLConnection connection;
    String result = "";

    try {

        url = new URL("https://bmorgan17.github.io/html-database");
    //OPEN UP CONNECTION WITH URL
        connection = (HttpURLConnection) url.openConnection();
    //SET UP INPUT STREAM FOR DATA
        InputStream in = connection.getInputStream();
    //SET UP READER TO READ THE INCOMING DATA
        InputStreamReader reader = new InputStreamReader(in);
    //INPUT STREAM GETS DATA AS CHARACTERS SO THIS READS THE FIRST CHARACTER
        int data = reader.read();
    //LOOP THROUGH EACH CHARACTER UNTIL YOU GET ALL CONTENT REQUESTED
        while (data != -1) {
            char c = (char) data;
     //CONCATANATE TO STRING
            result += c;
            data = reader.read();
        }

        Log.i("Result", result);

        } catch ( Exception ex ) {
             ex.printStackTrace();
             Log.wtf("FAILED", "Hard rip mate");
        }

答案 1 :(得分:0)

尝试添加:

http://watchout4snakes.com/wo4snakes/Random/RandomWord

之前

>>> import requests
>>> r = requests.post('http://watchout4snakes.com/wo4snakes/Random/RandomWord')
>>> print(r.text)
doom

编辑:

也尝试添加:

connection.setDoInput(true);
int statusCode = connection.getResponseCode();
Log.wtf("PROGRESS", "status code: " + statusCode);

之前:

Log.wtf("PROGRESS", "connect");

EDIT2:

我刚刚意识到您的网址以https开头,并且您使用HttpUrlConnection。将其更改为HttpsUrlConnection。