11

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.

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
kongkea
  • 9,788
  • 12
  • 39
  • 49

9 Answers9

12

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 because currently you are not making any post for getting Json data from the server and just trying to parse an empty string to json

Abayomi Israel
  • 589
  • 6
  • 14
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • it works but not display anything. and it say can't JSONObject can't be convert to JSONArray – kongkea Dec 26 '12 at 07:00
  • @kongkea : because you are downloading all images and json and then showing in GridView . solution is change your code using lazy loading to show images from web and just store image Url's in Map and in getView method of Adapter try to download require imagesloadBitmap – ρяσѕρєя K Dec 26 '12 at 07:15
  • can you give me some simple? or Did you have tutorial about Gridview get image from JSON using AsyncTask and Universal-Image-Loader? – kongkea Jan 09 '13 at 09:28
5

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.

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).

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

Community
  • 1
  • 1
thepoosh
  • 12,497
  • 15
  • 73
  • 132
3

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

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 :

{
    "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 
}
Deepak Kumar
  • 1,035
  • 10
  • 18
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • i assigned string result=""; and also doing JSONObject jsonObject = new JSONObject(result); and still getting errror –  Dec 27 '14 at 07:42
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.

1

-please check your all json veriables are properly return only json format or not.

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"));}
0

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

Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
0

Sometimes the probleme is the URL, if your website forces HTTPS, you have to make sure that you are using an HTTPS URL in your APP.

String url = "https://example.com/adchara1/";
Ryad Amir
  • 21
  • 4
-2
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);