简体   繁体   中英

Gson outputting com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 501

I've been using Gson to parse JSON in an API to use in my app. This is an example of the JSON output:

{"getdashboarddata":{"version":"1.0.0","runtime":492.3939704895,"data":{"raw":{"personal":{"hashrate":0},"pool":{"hashrate":1705841},"network":{"hashrate":41430764.301,"esttimeperblock":94.464466565347,"nextdifficulty":578.78109116,"blocksuntildiffchange":1}},"personal":{"hashrate":0,"sharerate":"0.0000","sharedifficulty":0,"shares":{"valid":0,"invalid":0,"invalid_percent":0,"unpaid":0},"estimates":{"block":0,"fee":0,"donation":0,"payout":0}},"pool":{"info":{"name":"RAPIDHASH","currency":"DOGE"},"workers":2199,"hashrate":1.705841,"shares":{"valid":1866290,"invalid":15564,"invalid_percent":0.83,"estimated":1866214,"progress":100},"price":"0.00000073","difficulty":32,"target_bits":21},"system":{"load":[0.1,0.25,0.29]},"network":{"hashrate":41.430764301,"difficulty":911.23745057,"block":235378,"esttimeperblock":94.46,"nextdifficulty":578.78109116,"blocksuntildiffchange":1}}}}

This is the logcat:

05-25 17:12:00.230  24143-24159/io.kd.figgycity50.mpos.mposchecker E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: io.kd.figgycity50.mpos.mposchecker, PID: 24143
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 501
        at com.google.gson.Gson.fromJson(Gson.java:813)
        at com.google.gson.Gson.fromJson(Gson.java:768)
        at com.google.gson.Gson.fromJson(Gson.java:717)
        at com.google.gson.Gson.fromJson(Gson.java:689)
        at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:77)
        at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:64)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: java.io.EOFException: End of input at line 1 column 501
        at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1377)
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:471)
        at com.google.gson.stream.JsonReader.skipValue(JsonReader.java:1209)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:170)
        at com.google.gson.Gson.fromJson(Gson.java:803)
            at com.google.gson.Gson.fromJson(Gson.java:768)
            at com.google.gson.Gson.fromJson(Gson.java:717)
            at com.google.gson.Gson.fromJson(Gson.java:689)
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:77)
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:64)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

This is the code that gets and parses the JSON:

private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            // params comes from the execute() call: params[0] is the url.
            String resp = null;
            try {
                resp =  downloadUrl(urls[0]);
            } catch (IOException e) {
                resp = "{\"getdashboarddata\":{\"data\":{\"personal\":{\"hashrate\":0},\"pool\":{\"info\":{\"name\":\"SET POOL IN SETTINGS\",\"currency\":\"???\"},\"hashrate\":0,\"difficulty\":0},\"network\":{\"hashrate\":0,\"difficulty\":0}}}}";
            }
            Gson gson = new Gson();
            Log.d("INFO", resp);
            MPOSDashStruct data = gson.fromJson(resp, MPOSDashStruct.class);
            TextView t = (TextView)findViewById(R.id.poolName);
            TextView t2 = (TextView)findViewById(R.id.khashMe);
            TextView t3 = (TextView)findViewById(R.id.khashPool);
            TextView t4 = (TextView)findViewById(R.id.khashNet);
            t.setText(data.getName());
            t2.setText(data.getMyHash());
            t3.setText(data.getPoolHash());
            t4.setText(data.getNetHash());
            return "Hello World. If you see this, you probably hack. Get out.";
        }

        private String downloadUrl(String myurl) throws IOException {
            InputStream is = null;
            // Only display the first 500 characters of the retrieved
            // web page content.
            int len = 500;

            try {
                URL url = new URL(myurl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                // Starts the query
                conn.connect();
                int response = conn.getResponseCode();
                is = conn.getInputStream();

                // Convert the InputStream into a string
                String contentAsString = readIt(is, len);
                return contentAsString;

                // Makes sure that the InputStream is closed after the app is
                // finished using it.
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }

        public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
            Reader reader = null;
            reader = new InputStreamReader(stream, "UTF-8");
            char[] buffer = new char[len];
            reader.read(buffer);
            return new String(buffer);
        }
    }
}

Here's the class MPOSDashStruct:

package io.kd.figgycity50.mpos.mposchecker;

import com.google.gson.annotations.SerializedName;

/**
 * Created by George on 25/05/2014.
 */
public class MPOSDashStruct {
    public static class getdashboarddata {
        public static class data {
            public static class personal {
                public static int hashrate;
            }
            public static class pool {
                public static int hashrate;
                public static int difficulty;
                public static class info {
                    @SerializedName("name")
                    public static String poolName;
                    public static String currency;
                }
            }
            public static class network {
                public static int hashrate;
                public static int difficulty;
            }
        }
    }
    public String getName() {
        return getdashboarddata.data.pool.info.poolName;
    }

    public String getCurrency() {
        return getdashboarddata.data.pool.info.currency;
    }

    public int getPoolHash() {
        return getdashboarddata.data.pool.hashrate;
    }

    public int getPoolDiff() {
        return getdashboarddata.data.pool.difficulty;
    }

    public int getMyHash() {
        return getdashboarddata.data.personal.hashrate;
    }

    public int getNetHash() {
        return getdashboarddata.data.network.hashrate;
    }
}

The code is seemingly good, and Android Studio has no errors.

EDIT 2: contentAsString ouputs

05-25 17:30:12.976  29974-29988/io.kd.figgycity50.mpos.mposchecker D/INFO﹕ {"getdashboarddata":{"version":"1.0.0","runtime":632.7919960022,"data":{"raw":{"personal":{"hashrate":0},"pool":{"hashrate":1675289},"network":{"hashrate":38541151.201,"esttimeperblock":61.161346587453,"nextdifficulty":538.41366248,"blocksuntildiffchange":1}},"personal":{"hashrate":0,"sharerate":"0.0000","sharedifficulty":0,"shares":{"valid":0,"invalid":0,"invalid_percent":0,"unpaid":0},"estimates":{"block":0,"fee":0,"donation":0,"payout":0}},"pool":{"info":{"name":"RAPIDHASH","currency":"DOGE"}

using Log.d("INFO", contentAsString)

EDIT: After using getContentLength, the new logcat is this:

05-25 17:50:19.203    5143-5157/io.kd.figgycity50.mpos.mposchecker W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41bd4ba8)
05-25 17:50:19.203    5143-5157/io.kd.figgycity50.mpos.mposchecker E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: io.kd.figgycity50.mpos.mposchecker, PID: 5143
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NegativeArraySizeException: -1
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.readIt(MainActivity.java:120)
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.downloadUrl(MainActivity.java:104)
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:71)
            at io.kd.figgycity50.mpos.mposchecker.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:64)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

which points to this line of my code:

public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
            Reader reader = null;
            reader = new InputStreamReader(stream, "UTF-8");
            char[] buffer = new char[len]; //this line, others added so you can understand
            reader.read(buffer);
            return new String(buffer);
        }

EDIT 3: Directing Gson to handle the Reader worked well, but new errors arise again. Logcat:

05-26 09:17:52.459  11810-11810/io.kd.figgycity50.mpos.mposchecker E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: io.kd.figgycity50.mpos.mposchecker, PID: 11810
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.kd.figgycity50.mpos.mposchecker/io.kd.figgycity50.mpos.mposchecker.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at io.kd.figgycity50.mpos.mposchecker.MainActivity.onCreate(MainActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

which points to:

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        String prefPool = sharedPref.getString("pref_pool", "https://doge.rapidhash.net/");
        String prefKey = sharedPref.getString("pref_key", "XXXX");
        new DownloadWebpageTask().execute(prefPool + "index.php?page=api&action=getdashboarddata&api_key=" + prefKey);
        TextView t = (TextView)findViewById(R.id.poolName); // this line
        TextView t2 = (TextView)findViewById(R.id.khashMe);
        TextView t3 = (TextView)findViewById(R.id.khashPool);
        TextView t4 = (TextView)findViewById(R.id.khashNet);
        t.setText(dashStruct.getName());
        t2.setText(dashStruct.getMyHash());
        t3.setText(dashStruct.getPoolHash());
        t4.setText(dashStruct.getNetHash());
    }

I don't know why you do this

// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;

If your JSON is longer than that, you're breaking it into pieces that are not parseable by JSON parsers. Read the full JSON string.

Get the content length from the response.

int len = conn.getContentLength();

From Edit 3 above, the following line seems to be throwing the NullPointerException :

t.setText(dashStruct.getName());

I can tell that you declare dashStruct as a class level variable which is getting initialized in the AsyncTask DownloadWebpageTask .

By design, an AsyncTask will be executed off the main thread (to be precise, the AsyncTask#doInBackground(T) method). If DownloadWebpageTask hasn't finished initializing dashStruct when the main thread executes t.setText(dashStruct.getName()); - an NPE will be thrown - dashStruct is null.

To get past this exception, override onPostExecute() in DownloadWebpageTask and move the following code to it (remove it from onCreate(Bundle) ):

@Override
protected void onPostExecute(String result) {

    if (dashStruct != null) {
        t.setText(dashStruct.getName());
        t2.setText(dashStruct.getMyHash());
        t3.setText(dashStruct.getPoolHash());
        t4.setText(dashStruct.getNetHash());
    } else {
        // Deal with dashStruct being null
    }
}

I notice that in your original posting, you are updating your UI (setting textview text) from the DownloadWebpageTask#doInBackground(String...) . This will throw an IllegalStateException as doInBackground() is processed off the main thread - the only thread allowed to touch your UI. onPostExecute() is processed on the main thread. And that's where your UI logic should go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM