简体   繁体   English

Gson输出com.google.gson.JsonSyntaxException:java.io.EOFException:第1行第501列的输入结束

[英]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. 我一直在使用Gson在一个API中解析JSON以在我的应用程序中使用。 This is an example of the JSON output: 这是JSON输出的示例:

{"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: 这是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: 这是获取并解析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: 这是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. 代码似乎很好,Android Studio没有错误。

EDIT 2: contentAsString ouputs 编辑2:contentAsString输出

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) 使用Log.d(“INFO”,contentAsString)

EDIT: After using getContentLength, the new logcat is this: 编辑:使用getContentLength后,新的logcat是这样的:

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. 编辑3:指导Gson处理Reader工作得很好,但新的错误再次出现。 Logcat: 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. 如果您的JSON比这长,那么您将其分解为JSON解析器无法解析的部分。 Read the full JSON string. 阅读完整的JSON字符串。

Get the content length from the response. 从响应中获取内容长度

int len = conn.getContentLength();

From Edit 3 above, the following line seems to be throwing the NullPointerException : 从上面的Edit 3 ,以下行似乎抛出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 . 我可以告诉你将dashStruct声明为一个类级变量,它在AsyncTask DownloadWebpageTask初始化。

By design, an AsyncTask will be executed off the main thread (to be precise, the AsyncTask#doInBackground(T) method). 按照设计,将在主线程(确切地说,AsyncTask#doInBackground(T)方法)之外执行AsyncTask。 If DownloadWebpageTask hasn't finished initializing dashStruct when the main thread executes t.setText(dashStruct.getName()); 如果在主线程执行t.setText(dashStruct.getName());时, DownloadWebpageTask尚未完成初始化dashStruct t.setText(dashStruct.getName()); - an NPE will be thrown - dashStruct is null. - 将抛出NPE - dashStruct为null。

To get past this exception, override onPostExecute() in DownloadWebpageTask and move the following code to it (remove it from onCreate(Bundle) ): 为了突破这个例外,覆盖onPostExecute()DownloadWebpageTask和下面的代码移动到它(删除从它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...) . 我注意到在原始帖子中,您正在从DownloadWebpageTask#doInBackground(String...)更新UI(设置textview文本DownloadWebpageTask#doInBackground(String...) This will throw an IllegalStateException as doInBackground() is processed off the main thread - the only thread allowed to touch your UI. 这将抛出IllegalStateException因为doInBackground()是从主线程处理的 - 唯一允许触摸UI的线程。 onPostExecute() is processed on the main thread. onPostExecute()在主线程上处理。 And that's where your UI logic should go. 这就是你的UI逻辑应该去的地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.EOFException:Gson解析器中第1行第1行路径$的输入结束 - java.io.EOFException: End of input at line 1 column 1 path $ in Gson parser com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第2行第5列 - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 5 gson错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第166列处为STRING - gson error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 166 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第1列路径$ STRING - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ 改型错误com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第1列路径$ STRING - Retrofit Error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2列是BEGIN_ARRAY - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第1列的路径$ - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径处为BEGIN_ARRAY - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 引起原因:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第2列 - Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为字符串,但在第 3 列路径 $message - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM