Org. json. JSONException: Endofinputatcharacter0of

Source: Internet
Author: User
When I wrote the code today, I also encountered the following error message: 04-0821: 55: 22.792: W/System. err (10665): org. json. JSONException: Endofinputatcharacter0of04-0821: 55: 22.802: W/System. err (10665): atorg. j...

When I wrote the code today, I also encountered the following error message:

04-08 21:55:22. 792: W/System. err (10665): org. json. JSONException: End of input at character 0
04-08 21:55:22. 802: W/System. err (10665): at org. json. JSONTokener. syntaxError (JSONTokener. java: 446)
04-08 21:55:22. 802: W/System. err (10665): at org. json. JSONTokener. nextValue (JSONTokener. java: 93)
04-08 21:55:22. 802: W/System. err (10665): at org. json. JSONArray. (JSONArray. java: 87)
04-08 21:55:22. 812: W/System. err (10665): at org. json. JSONArray. (JSONArray. java: 103)
04-08 21:55:22. 812: W/System. err (10665): at cn. jbit. service. GetNews. GetJSON (GetNews. java: 65)
04-08 21:55:22. 812: W/System. err (10665): at cn. jbit. service. GetNews. getNewsListByJSON (GetNews. java: 48)
04-08 21:55:22. 812: W/System. err (10665): at cn. jbit. news. NewsActivity. fillListView (NewsActivity. java: 29)
04-08 21:55:22. 812: W/System. err (10665): at cn. jbit. news. NewsActivity. onCreate (NewsActivity. java: 24)
04-08 21:55:22. 812: W/System. err (10665): at android. app. Instrumentation. callActivityOnCreate (Instrumentation. java: 1047)
04-08 21:55:22. 812: W/System. err (10665): at android. app. ActivityThread. initialize mlaunchactivity (ActivityThread. java: 1611)
04-08 21:55:22. 812: W/System. err (10665): at android. app. ActivityThread. handleLaunchActivity (ActivityThread. java: 1663)
04-08 21:55:22. 812: W/System. err (10665): at android. app. ActivityThread. access $1500 (ActivityThread. java: 117)
04-08 21:55:22. 822: W/System. err (10665): at android. app. ActivityThread $ H. handleMessage (ActivityThread. java: 931)
04-08 21:55:22. 822: W/System. err (10665): at android. OS. Handler. dispatchMessage (Handler. java: 99)
04-08 21:55:22. 822: W/System. err (10665): at android. OS. low.loop (low.java: 123)
04-08 21:55:22. 822: W/System. err (10665): at android. app. ActivityThread. main (ActivityThread. java: 3683)
04-08 21:55:22. 832: W/System. err (10665): at java. lang. reflect. Method. invokeNative (Native Method)
04-08 21:55:22. 832: W/System. err (10665): at java. lang. reflect. Method. invoke (Method. java: 507)
04-08 21:55:22. 832: W/System. err (10665): at com. android. internal. OS. ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 839)
04-08 21:55:22. 832: W/System. err (10665): at com. android. internal. OS. ZygoteInit. main (ZygoteInit. java: 597)
04-08 21:55:22. 832: W/System. err (10665): at dalvik. system. NativeStart. main (Native Method)

Let's first translate the meaning of the error message: A JSONException is thrown at the end of the input 0 character;

Later, I found the cause of the error:

/**
* Read Stream Data
* @ Param input
* @ Return
* @ Throws Exception
*/
Public static byte [] read (InputStream input) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream ();
Byte [] data = new byte [1024];
Int len = 0;
While (len = input. read (data ))! =-1 ){
Out. write (data, 0, 0); // it should have been out. write (data, 0, len );
}
Out. close ();
Return out. toByteArray ();
}


After reading the above code, I think you know why I threw this exception. Then let's look at the following code:

/***
* Return JSON data from the server and parse it here
* @ Param input
* @ Return
* @ Throws Exception
*/
Private static List GetJSON (InputStream input) throws Exception {
List List = new ArrayList ();
Byte [] data = StreamTool. read (input );
String json = new String (data); // The data produced from the server is encoded as UTF-8. This is not written here.
JSONArray array = new JSONArray (json );
For (int I = 0; iJSONObject jsonObject = array. getJSONObject (I );
News news = new News ();
News. setNid (jsonObject. getInt ("id "));
News. setNauthor (jsonObject. getString ("author "));
News. setNtitle (jsonObject. getString ("title "));
List. add (news );
}
Return list;
}


I called the first read (InputStream input) method, because I accidentally typed the error code, the value returned by this method must be any byte, the parsing of JSON is certainly abnormal when the index is 0!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.