How to use Json parser android?

Posted on Nov 14, 2013 10:08 AM, Android - Stack Overflow | stackoverflow.com

I used the parser given below with an example Android 2.2. My example is working and i have no problem. when tried to use the some example in a project using Android 4.3. but i have a problem with my parser line

HttpResponse httpResponse = httpClient.execute(httpPost);

I checked all the permission in my manifest, all of them are used.

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = '';

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            Log.d('test url' , 'le request est lancé') ;
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.d('test url' , 'la connection etablie');
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, 'iso-8859-1'), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + '\n');
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e('Buffer Error', 'Error converting result ' + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e('JSON Parser', 'Error parsing data ' + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

the log is:

11-14 10:48:38.590: ERROR/AndroidRuntime(3801): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.web.tab/fr.web.profilconfiguration.AndroidJSONParsingActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
        at java.net.InetAddress.getAllByName(InetAddress.java:220)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at fr.web.profilconfiguration.JSONParser.getJSONFromUrl(JSONParser.java:44)
        at fr.web.profilconfiguration.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:62)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
Tagged android json

Similar Messages
  1. Android Json Parser parses not all objects
  2. Have anyone implemented Load more paging in android with JSON parser ?
  3. Why we could not use the net.sf.json in android?
  4. How to get a random object from text file using JSON in android
  5. Using Json with android app
  6. Retrieve Data by using json parser & pass a parameter to url in Android
  7. Json Parser in android
  8. JSON-RPC Android
  9. Error Parsing data with json in Android Login Project
  10. JSON Parser Error : http client failure
  11. Json in Android not working as it sould
  12. Populate listview with json parser
  13. How can I use the jenkins android lint plugin in my android project ,what can I write in bulid.xml? Thank you
  14. How to use MigLayout in Android
  15. Use webrtc in android app
  16. Trying to parse JSON in Android with gson. Keep getting a crash
  17. How to retrieve base64 encoded JSON to Android
  18. Notifications about new offers using JSON in Android
  19. Receive Notifications about new offers using JSON in Android

Android Json Parser parses not all objects

Posted on Nov 30, 2013 2:13 AM,

I don't understand why my Json parser in my Android App doesn't parses all Objects. My parser parses only the first 27 items of a public restfull Api at http://api.opendevicelab.com . Here's my Parser Code: private...

Have anyone implemented Load more paging in android with JSON parser ?

Posted on Jan 27, 2014 1:09 AM,

I wanna implement a load more pagination in android for my listview with JSON Parser. I got one tutorial but its with XML parser from here. http://www.androidhive.info/2012/03/android-listvi...

Why we could not use the net.sf.json in android?

Posted on Mar 06, 2013 1:55 AM,

If we use net.sf.json.JSONObejct in android ,the class loader can not find the class . Why we could not use the net.sf.json in android? and have a json jar which could use in android ? becuse the org.json of google is too simple . i want to parse a JSONObject to/from an Object. thxs a lot . ...

How to get a random object from text file using JSON in android

Posted on Nov 23, 2013 4:15 AM,

I am developing MCQ quiz application in android which contain different category like php , asp.net etc. I have successfully completed it but i want to fetch the question randomly without repeating question from text file using json parser how can i do it ...

Using Json with android app

Posted on Dec 13, 2013 3:22 AM,

I want to use json in order to import info from my ms-sql database to my android app. So how do I do it? I am using this line to convert my data to json: string json = JsonConvert.SerializeObject(info); And what now? Where do I save this serialized data and how to ...

Retrieve Data by using json parser & pass a parameter to url in Android

Posted on Dec 29, 2013 8:48 AM,

What i need is to append a parameter to url to retrieve data & I am using json parser. Getting a perfect result where i don't have to pass a parameter. But not being able to figure out how to pass parameter like String ID to url to retrieve data. ...

Json Parser in android

Posted on May 06, 2014 7:58 AM,

http://www.example.com/?p=115 Am trying to parse such url using json parser class below the class works for normal wordpress urls like this one. ...

JSON-RPC Android

Posted on Jul 02, 2014 5:24 AM,

I'm trying to use JSON-RPC for Android, I test this code individually in Eclipse and this works but when I test this code in Android Studio (API 19) this returns "{"error":{"code":591,"msg":"method not found (session may have timed out)"}}" private static JSONObject performSIARequest2() ...

Error Parsing data with json in Android Login Project

Posted on Jul 15, 2014 2:17 AM,

Why with my code below, I am trying to create a login and sign up form using a database with the use of JSON parser but after the run there is an error, the following error in its log: 07-15 01:55:47.400: E/JSON(1661): <HTML></HTML> 07-15 01:55:47.400: E/JSON Parser(1661): Error...

JSON Parser Error : http client failure

Posted on Nov 22, 2013 3:33 AM,

Im learning the JSON Parser. I follow the androidhive tutorial and copy its code, but sadly, i cannot. It does not display anything and has these type of errors: 11-22 17:27:12.132: E/AndroidRuntime(1938): FATAL EXCEPTION: main 11-22 17:27:12.132: E/AndroidRuntime(1938): java.l...

Json in Android not working as it sould

Posted on Dec 08, 2013 3:31 AM,

I have a problem with Json in android. In 4+ it works like a charm but in 2.2 it fails. I'm really stuck here. I get error I get error: java.lang.ClassCastException: java.lang.String In my server I parse array as: [{"PRODUCT": {"product_id":"1", "name":"name1"}}, {...

Populate listview with json parser

Posted on Dec 13, 2013 1:50 AM,

I want to populate listview with json parser. but i have following error: 12-12 22:49:39.812: ERROR/JSON Parser(1254): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject JSONParse: public...

How can I use the jenkins android lint plugin in my android project ,what can I write in bulid.xml? Thank you

Posted on Dec 19, 2013 8:56 AM,

I want to use the jenkins android lint plugin in my android project , but I don't know what can I do , something write in android project build.xml? please help me . thank you --zhfliu...

How to use MigLayout in Android

Posted on Dec 26, 2013 1:29 AM,

I am new To android and I want to use MigLayout in Android. 1.how to use the miglayout tag in xml layout. 2.can i use my existing layout with Miglayout. <miglayout> <relative layout> </relative layout> </miglayout> please he...

Use webrtc in android app

Posted on Dec 26, 2013 2:57 AM,

In my app I want to enable users to do Real time chat with each other,just like skype or some other app. Webrtc seems build mostly for web browser,I know it is great and support android and ios,but is it wisely to use that in android(and ios)apps? What I want to know is: Is there...

Trying to parse JSON in Android with gson. Keep getting a crash

Posted on Jan 15, 2014 10:43 AM,

I'm getting a crash everytime I try to parse this JSON in Android. I'm using GSON because of the Ion library I'm using to do async network calls. Here is my async ion call: @Override protected Void doInBackground(Void... params) { Ion.with(myActivity.this, ...

How to retrieve base64 encoded JSON to Android

Posted on Feb 01, 2014 12:22 PM,

How to retrieve the image_url from json to android.json code is here. I just want to get the image to a button. { "page_menu": { "flag": "M" }, "menu": [ { "pid": "0", "name": "Home", "refid": "1", "image_url": "aG9...

Notifications about new offers using JSON in Android

Posted on Feb 24, 2014 3:26 AM,

Hi all i just started android development, and my target is to create a sample program to get notifications using JSON, I know how to parse JSON in android, and now my task is to develop an app to get Notifications about new offers I am looking for some...

Receive Notifications about new offers using JSON in Android

Posted on Feb 24, 2014 3:26 AM,

Hi all i just started android development, and my target is to create a sample program to get notifications using JSON, I know how to parse JSON in android, and now my task is to develop an app to get Notifications about new offers I am looking for some...

 
 
Email:

Username:

Password:


Register

Username:

Password:

Login