0

我正在为一个计算项目编写一个 Android 应用程序。我有一段代码可以下载网页的 html 并将其存储为字符串,但它不会下载整个 html 正文。

下面是我正在使用的代码:

package com.example.pete.computingproject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import android.preference.PreferenceManager;
import android.content.SharedPreferences;


public class async extends AsyncTask<String, String, String> {
private Context mContext;

public async(Context context) {
    mContext = context;
}
@Override

protected String doInBackground(String... strings) {

    String content = null;
    URLConnection connection = null;


    // TODO Auto-generated method stub

    try {
        connection = new URL("https://stars.cirencester.ac.uk/index_bypass.php?view=tab_content/timetable&wk=6&stu_id=142669").openConnection();
        Scanner scanner = new Scanner(connection.getInputStream());
        scanner.useDelimiter("\\Z");
        content = scanner.next();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println(content);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("timetable1", content);
    editor.apply();
    return (content);




}


}

我要下载以下页面的源码(右键,查看源码) https://stars.cirencester.ac.uk/index_bypass.php?view=tab_content/timetable&wk=6&wk_num=&year=2015&stu_id=142669

但是,由于某种原因,我正在使用的代码在第 40 行停止。我需要所有这些代码才能使我的应用程序正常工作。任何帮助,将不胜感激!如果可能的话,我只想对我当前的代码进行细微的调整。但是,如果不使用完全不同的代码就无法做到这一点,请告诉我。谢谢!

4

1 回答 1

0

我建议使用依赖项“OkHttp”,而不是在套接字上打破你的头,为你完成了这项工作。

添加依赖: 编译'com.squareup.okhttp:okhttp:2.5.0'

了解更多关于如何使用它 从这里了解更多信息:https ://github.com/square/okhttp/wiki/Recipes

于 2015-09-26T17:45:02.043 回答