-2

I want to get JSON from HTTP and parse it into an object in my Android app. I've tried Spring Framework with AsyncTask, but everyone says it's deprecated.

So how can I make this happen, or does someone have a link to a tutorial for me?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Stefan van de Laarschot
  • 2,154
  • 6
  • 30
  • 50

4 Answers4

2

I would extend Denis proposal with this article and when to use given library.

First of all article from few months ago JSON Libraries Performance Comparison

If you dont wanna read:

  • Use Jackson library for big JSON data
  • Use GSON for small JSON data ( Most of the requests are rather small )
Matuszew
  • 841
  • 5
  • 12
1

refer this, it will give you response in String, you can convert it in jsonObject

public static String postUrlResponse(String url,List<NameValuePair> urlParameters) 
{
    try
    {
        System.out.println("URL : " + url);
        HttpParams httpParams = new BasicHttpParams();
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost post = new HttpPost(url);

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + post.getEntity());
        System.out.println("Response Code : " + 
                response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return e.toString();
    }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

Best library for parsing JSON is Jackson.
Here is very helpful tutorials about this. According to this article you can see Jackson is the fastest

Dennis Zinkovski
  • 1,821
  • 3
  • 25
  • 42
0

Answer to my own question thanks everyone.

public static List<Events> getEvents() {

    List<Events> ret = new ArrayList<Events>();
    ProgressDialog pr = new ProgressDialog(context);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);


    // YOUR URL GOES HERE
    String getUrl = "http://example.nl/CALL.ashx?Onderdeel=Feeds&AuthToken=" + WWIBCore.AuthToken;
    HttpClient http = new DefaultHttpClient();

    HttpResponse response = null;
    HttpGet getMethod = new HttpGet(getUrl);
    try {
        response = http.execute(getMethod);

        // CONVERT RESPONSE TO STRING
        String result = EntityUtils.toString(response.getEntity());

        // CONVERT RESPONSE STRING TO JSON ARRAY
        JSONArray ja = new JSONArray(result);

        // ITERATE THROUGH AND RETRIEVE EVENTS FIELDS
        int n = ja.length();

        for (int i = 0; i < n; i++) {

            // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
            JSONObject jo = ja.getJSONObject(i);

            // CONVERT DATA FIELDS TO EVENTS OBJECT
            Events e = new Events();
            e.setNaam(jo.getString(("Naam")));
            e.setContent(jo.getString("Content"));

            ret.add(e);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    pr.dismiss();
    return ret;
}
Stefan van de Laarschot
  • 2,154
  • 6
  • 30
  • 50