1

Here is the code:

    @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;

}

I'm basically downloading the JSON data from Google Spreadsheets, but the result returned from the doInBackground function continues to return some sort of a null object reference. What does this mean?...

EDIT* Here is my logcat:

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) 

Picture of what is actually being stored inside String result... enter image description here

LinearM
  • 133
  • 1
  • 7
  • 1
    How do you know this? Post your logcat? – Ishita Sinha Jul 28 '16 at 05:54
  • I added my logcat to the post. I added an alertDialog to print out (test if the string is there or not...) the string result which would be returned by my doInBackground function. Then that result should have been available to my postExecute function later on, so that I can convert the result to a JSONobject. – LinearM Jul 28 '16 at 19:43

2 Answers2

2

My problem was:

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

I found out that by reading here: 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)

I changed the statement to:

URL url = new URL("https://spreadsheets.google.com/tq?key=BLAHBLOAHBLAH");
Community
  • 1
  • 1
LinearM
  • 133
  • 1
  • 7
0

Your code looks fine, the problem is that you are not allowed to get data from that URL.

On hitting this URL "http://spreadsheets.google.com/tq?key=BLAHBLOAHBLAH"

I am getting following response, so i guess you are getting access denied from server.

google.visualization.Query.setResponse (
{
   "version": "0.6",
   "status": "error",
   "errors": [
   {
     "reason": "access_denied",
     "message": "Access denied",
     "detailed_message": "Access denied"
   }]
}
)
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • 1
    The google spreadsheets I put in my code is a dummy link. The actual link I allowed everyone to edit and modify as long as they have the link. – LinearM Jul 28 '16 at 19:33