org.json。JSONException:字符输入结束。

时间:2022-10-29 14:33:33

when I try to get json that doesn't have object, it works fine. but when I try go get json that has an object it's getting error.

当我试图获取没有对象的json时,它运行得很好。但是当我尝试获取一个json对象时,它会出错。

Have you got any idea?

你有什么主意吗?

protected Void doInBackground(String... params) {

    String url = "http://example.com/adchara1/";
    JSONArray data = null;

    try {
        JSONObject jsonObject = new JSONObject(result);

        MyArrList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map;
        data = jsonObject.getJSONArray("countries");
        data = new JSONArray(getJSONUrl(url));
        for (int i = 0; i < data.length(); i++) {
            JSONObject c = data.getJSONObject(i);
            map = new HashMap<String, Object>();

            // Thumbnail Get ImageBitmap To Object
            map.put("photo", (String) c.getString("photo"));
            map.put("ImageThumBitmap",(Bitmap) loadBitmap(c.getString("photo")));

            // Full (for View Popup)
            map.put("frame", (String) c.getString("frame"));

            MyArrList.add(map);
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

error

错误

12-26 11:51:10.352: W/System.err(23284): org.json.JSONException: End of input at character 0 of 
12-26 11:51:10.368: W/System.err(23284):    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
12-26 11:51:10.368: W/System.err(23284):    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
12-26 11:51:10.368: W/System.err(23284):    at org.json.JSONObject.<init>(JSONObject.java:154)
12-26 11:51:10.368: W/System.err(23284):    at org.json.JSONObject.<init>(JSONObject.java:171)
12-26 11:51:10.376: W/System.err(23284):    at com.example.gridview.MainActivity$DownloadJSONFileAsync.doInBackground(MainActivity.java:180)
12-26 11:51:10.376: W/System.err(23284):    at com.example.gridview.MainActivity$DownloadJSONFileAsync.doInBackground(MainActivity.java:1)
12-26 11:51:10.376: W/System.err(23284):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-26 11:51:10.376: W/System.err(23284):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-26 11:51:10.376: W/System.err(23284):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-26 11:51:10.376: W/System.err(23284):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-26 11:51:10.376: W/System.err(23284):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-26 11:51:10.376: W/System.err(23284):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-26 11:51:10.376: W/System.err(23284):    at java.lang.Thread.run(Thread.java:856)
12-26 11:51:10.399: D/libEGL(23284): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
12-26 11:51:10.423: D/libEGL(23284): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
12-26 11:51:10.430: D/libEGL(23284): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
12-26 11:51:10.516: D/OpenGLRenderer(23284): Enabling debug mode 0
12-26 11:51:10.735: D/AndroidRuntime(23284): Shutting down VM
12-26 11:51:10.735: W/dalvikvm(23284): threadid=1: thread exiting with uncaught exception (group=0x4158f300)
12-26 11:51:10.735: E/AndroidRuntime(23284): FATAL EXCEPTION: main
12-26 11:51:10.735: E/AndroidRuntime(23284): java.lang.NullPointerException
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.example.gridview.MainActivity$ImageAdapter.getCount(MainActivity.java:122)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.widget.GridView.setAdapter(GridView.java:182)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.example.gridview.MainActivity.ShowAllContent(MainActivity.java:91)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.example.gridview.MainActivity$DownloadJSONFileAsync.onPostExecute(MainActivity.java:209)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.example.gridview.MainActivity$DownloadJSONFileAsync.onPostExecute(MainActivity.java:1)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.os.AsyncTask.finish(AsyncTask.java:631)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.os.Looper.loop(Looper.java:137)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at android.app.ActivityThread.main(ActivityThread.java:4931)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at java.lang.reflect.Method.invoke(Method.java:511)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-26 11:51:10.735: E/AndroidRuntime(23284):    at dalvik.system.NativeStart.main(Native Method)
12-26 11:51:12.509: I/Process(23284): Sending signal. PID: 23284 SIG: 9

7 个解决方案

#1


6  

Change

改变

 JSONObject jsonObject = new JSONObject(result);

to

result=getJSONUrl(url);  //<< get json string from server
JSONObject jsonObject = new JSONObject(result);

inside doInBackground method of DownloadJSONFileAsync becuase currently you are not making any post for getting Json data from server and just trying to parse empty string to josn

在DownloadJSONFileAsync becuase的doInBackground方法中,目前您并没有从服务器获取Json数据的任何post,只是试图解析空字符串到josn。

#2


2  

I'm not seeing you assign anything to result after this String result = ""; in here pastebin.com/mNVGEnD5 and if you trying to get json object with this

在这个字符串结果= ""之后,我没有看到你分配任何结果。这里是pastebin.com/mNVGEnD5如果你想用这个来获取json对象。

JSONObject jsonObject = new JSONObject(result);

it'll surely return end of input at character 0 because the string result is empty maybe you can try to replace it with some jsonString you can use this as an example :

它肯定会返回字符0的输入端因为字符串结果是空的也许你可以试着用一些jsonString替换它你可以用这个作为例子:

{
    "result": "OK",
    "member_id": "32",
    "first_name": "Android",
    "last_name": "JSON",
    "nickname": "HEY",
    "accesskey": "123556332",
    "facebook_id": "0",
    "facebook_token_expire_date": "0000-00-00 00:00:00",
    "facebook_token_expire_date2": "0000000000000",
    "facebook_token_refresh_date": "0000-00-00 00:00:00",
    "facebook_token_key": "null",
    "status_active": 1 
}

#3


1  

as you can see from this answer, you are probably getting a blank response.
in general, the org.json.JSONException: End of input at character N of... means that the JSON couldn't be parsed and something is not being done right.

正如您从这个答案中看到的,您可能得到了一个空白的响应。一般来说,org.json。JSONException:输入字符N(…)这意味着JSON不能被解析,而有些事情做得不对。

if the problem is that you're getting a blank response, you should make sure you get a response from the server before trying to parse it (like ρяσѕρєя K's answer).

如果问题是,你得到一个空白的反应,你应该确保你得到响应从服务器在解析之前(如ρяσѕρєяK的回答)。

if the problem is a bad JSON, you should try and validate it. JSONLint is a very good place to start.

如果问题是一个糟糕的JSON,您应该尝试验证它。JSONLint是一个很好的起点。

#4


1  

Please check PHP File URL in Browser if get the data means you have problem in android side if browser not shows the data it means problem in PHP file.

请在浏览器中查看PHP文件URL,如果得到数据意味着在android方面有问题,如果浏览器没有显示数据,这意味着PHP文件中的问题。

#5


0  

carid = new ArrayList<String>();
        carlogo = new ArrayList<String>();
        carname = new ArrayList<String>();
        prize = new ArrayList<String>();
        availibility = new ArrayList<String>();

        ListView carlist=(ListView)findViewById(R.id.listcardetails);
        adapter=new CustomAdapter(this, carid, carlogo, carname, prize, availibility);
        carlist.setAdapter(adapter);

#6


0  

JSONArray array=    new JSONArray(resultdata);
            for(int i=0;i<array.length();i++)
             {                  
                carid.add(jsonObject.getString("Id"));
                carlogo.add(jsonObject.getString("CarImage"));
                carname.add(jsonObject.getString("CarType"));
                prize.add(jsonObject.getString("Price"));
                availibility.add(jsonObject.getString("CarAvailable"));}

#7


0  

It is better to use optJSONObject instead of getJSONObject because it will not through exception.

最好使用optJSONObject而不是getJSONObject,因为它不会通过异常。

#1


6  

Change

改变

 JSONObject jsonObject = new JSONObject(result);

to

result=getJSONUrl(url);  //<< get json string from server
JSONObject jsonObject = new JSONObject(result);

inside doInBackground method of DownloadJSONFileAsync becuase currently you are not making any post for getting Json data from server and just trying to parse empty string to josn

在DownloadJSONFileAsync becuase的doInBackground方法中,目前您并没有从服务器获取Json数据的任何post,只是试图解析空字符串到josn。

#2


2  

I'm not seeing you assign anything to result after this String result = ""; in here pastebin.com/mNVGEnD5 and if you trying to get json object with this

在这个字符串结果= ""之后,我没有看到你分配任何结果。这里是pastebin.com/mNVGEnD5如果你想用这个来获取json对象。

JSONObject jsonObject = new JSONObject(result);

it'll surely return end of input at character 0 because the string result is empty maybe you can try to replace it with some jsonString you can use this as an example :

它肯定会返回字符0的输入端因为字符串结果是空的也许你可以试着用一些jsonString替换它你可以用这个作为例子:

{
    "result": "OK",
    "member_id": "32",
    "first_name": "Android",
    "last_name": "JSON",
    "nickname": "HEY",
    "accesskey": "123556332",
    "facebook_id": "0",
    "facebook_token_expire_date": "0000-00-00 00:00:00",
    "facebook_token_expire_date2": "0000000000000",
    "facebook_token_refresh_date": "0000-00-00 00:00:00",
    "facebook_token_key": "null",
    "status_active": 1 
}

#3


1  

as you can see from this answer, you are probably getting a blank response.
in general, the org.json.JSONException: End of input at character N of... means that the JSON couldn't be parsed and something is not being done right.

正如您从这个答案中看到的,您可能得到了一个空白的响应。一般来说,org.json。JSONException:输入字符N(…)这意味着JSON不能被解析,而有些事情做得不对。

if the problem is that you're getting a blank response, you should make sure you get a response from the server before trying to parse it (like ρяσѕρєя K's answer).

如果问题是,你得到一个空白的反应,你应该确保你得到响应从服务器在解析之前(如ρяσѕρєяK的回答)。

if the problem is a bad JSON, you should try and validate it. JSONLint is a very good place to start.

如果问题是一个糟糕的JSON,您应该尝试验证它。JSONLint是一个很好的起点。

#4


1  

Please check PHP File URL in Browser if get the data means you have problem in android side if browser not shows the data it means problem in PHP file.

请在浏览器中查看PHP文件URL,如果得到数据意味着在android方面有问题,如果浏览器没有显示数据,这意味着PHP文件中的问题。

#5


0  

carid = new ArrayList<String>();
        carlogo = new ArrayList<String>();
        carname = new ArrayList<String>();
        prize = new ArrayList<String>();
        availibility = new ArrayList<String>();

        ListView carlist=(ListView)findViewById(R.id.listcardetails);
        adapter=new CustomAdapter(this, carid, carlogo, carname, prize, availibility);
        carlist.setAdapter(adapter);

#6


0  

JSONArray array=    new JSONArray(resultdata);
            for(int i=0;i<array.length();i++)
             {                  
                carid.add(jsonObject.getString("Id"));
                carlogo.add(jsonObject.getString("CarImage"));
                carname.add(jsonObject.getString("CarType"));
                prize.add(jsonObject.getString("Price"));
                availibility.add(jsonObject.getString("CarAvailable"));}

#7


0  

It is better to use optJSONObject instead of getJSONObject because it will not through exception.

最好使用optJSONObject而不是getJSONObject,因为它不会通过异常。