Reputation: 9978
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
Upvotes: 11
Views: 89355
Reputation: 21
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/";
Upvotes: 0
Reputation: 19
-please check your all json veriables are properly return only json format or not.
Upvotes: 1
Reputation: 132992
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
Upvotes: 13
Reputation: 11122
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
}
Upvotes: 3
Reputation: 3729
It is better to use
optJSONObject
instead of getJSONObject
because it will not through exception.
Upvotes: 0
Reputation: 45
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.
Upvotes: 1
Reputation: 71
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"));}
Upvotes: 0
Reputation: 71
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);
Upvotes: -2
Reputation: 12587
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.
Upvotes: 5