android - AsyncTask DoInBackground 下载谷歌电子表格数据返回空值?

标签 android json android-asynctask google-sheets

代码如下:

    @Override
public String doInBackground(String... params)
{
    try
    {
        URL url = new URL("http://spreadsheets.google.com/tq?key=BLAHBLOAHBLAH");

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); //Cast HttpUrlConnection because URL does not contain methods available to url, creates object
        httpURLConnection.setRequestMethod("GET"); //Request data from source
        httpURLConnection.setDoInput(true);
        httpURLConnection.connect(); //Object actually connects, connect() invoked by getInputStream() etc.

        //Reads data from network stream
        InputStream inputStream = httpURLConnection.getInputStream();

        //Rather than read one character at a time from the network or disk, BufferedReader reads a larger block at a time
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        //Used to create mutable string e.g. append()
        StringBuilder stringBuilder = new StringBuilder();
        String line = "";

        //Change stream data to StringBuilder data
        while ((line = bufferedReader.readLine()) != null)
        {
            stringBuilder.append(line + "\n");
        }

        String result = stringBuilder.toString();

        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();

        return result;

    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    return null;

}

我基本上是从 Google Spreadsheets 下载 JSON 数据,但从 doInBackground 函数返回的结果继续返回某种空对象引用。这是什么意思?...

编辑* 这是我的日志:

07-28 12:41:30.289 3796-3891/com.example.steven.app E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                        Process: com.example.steven.app, PID: 3796
                                                                        java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                            at java.lang.Thread.run(Thread.java:818)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlertDialog.setMessage(java.lang.CharSequence)' on a null object reference
                                                                            at com.example.steven.database.DownloadGS.doInBackground(DownloadGS.java:95)
                                                                            at com.example.steven.database.DownloadGS.doInBackground(DownloadGS.java:49)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                            at java.lang.Thread.run(Thread.java:818) 

实际存储在字符串结果中的图片... enter image description here

最佳答案

我的问题是:

URL url = new URL("http://spreadsheets.google.com/tq?key=BLAHBLOAHBLAH");

我通过阅读这里发现: URLConnection Doesn't Follow Redirect

the setFollowRedirect and setInstanceFollowRedirects only work automatically when the redirected protocol is same . ie from http to http and https to https. (Shalvika)

我将语句更改为:

URL url = new URL("https://spreadsheets.google.com/tq?key=BLAHBLOAHBLAH");

关于android - AsyncTask DoInBackground 下载谷歌电子表格数据返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38627850/

相关文章:

javascript - 通过 JavaScript 在 Android 浏览器中禁用触摸手势

java - 反序列化 GSON 中对象的 JSON 集合(可能与另一个集合)

javascript - JSON相关错误: Unexpected token o

android - 使用 dialogFragment 自定义进度条对话框

android - 如何正确管理Android后台任务?

android - 异步任务异常

android - 在单例私有(private)构造函数类上使用 hilt 进行依赖注入(inject)

android - Google Pay(Tez) 集成到我的支付网关中吗? (安卓)

android - 升级到 SDK 21 - 膨胀类 android.support.v7.internal.widget.ActionBarContainer 时出错

java - 重命名通用类中的元素名称