1. Are you ready for the Galaxy S20? Here is everything we know so far!

connectivity through json

Discussion in 'Introductions' started by Shruti Shukla, Apr 11, 2012.

  1. Shruti Shukla

    Shruti Shukla Lurker
    Thread Starter

    public static JSONObject connect(String url)
    {
    HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    // Execute the request
    HttpResponse response;
    JSONObject json = new JSONObject();

    try {
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
    // A Simple JSON Response Read
    InputStream instream = entity.getContent();
    String result= convertStreamToString(instream);

    json=new JSONObject(result);

    instream.close();
    }


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

    return json;
    }


    ////////////////////////////////////for getting data in json arry formate


    public static JSONArray connectArray(String url)
    {
    HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    // Execute the request
    HttpResponse response;
    // JSONObject json = new JSONObject();
    JSONArray jsonArray= new JSONArray();
    InputStream instream=null;
    try {
    response = httpclient.execute(httpget);


    HttpEntity entity = response.getEntity();

    if (entity != null) {
    // A Simple JSON Response Read
    instream = entity.getContent();
    result= convertStreamToString(instream);


    jsonArray= new JSONArray(result);

    // json=new JSONObject(result);

    instream.close();
    }

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






    public static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
    while ((line = reader.readLine()) != null) {
    sb.append(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return sb.toString();
    }










































    return null;
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }

    return jsonArray;
    // return json;
    }


    public static JSONObject connect(String url)
    {
    HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    // Execute the request
    HttpResponse response;
    JSONObject json = new JSONObject();

    try {
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
    // A Simple JSON Response Read
    InputStream instream = entity.getContent();
    String result= convertStreamToString(instream);

    json=new JSONObject(result);

    instream.close();
    }


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

    return json;
    }





    public static JSONArray connectArray(String url)
    {
    HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    // Execute the request
    HttpResponse response;
    // JSONObject json = new JSONObject();
    JSONArray jsonArray= new JSONArray();
    InputStream instream=null;
    try {
    response = httpclient.execute(httpget);


    HttpEntity entity = response.getEntity();

    if (entity != null) {
    // A Simple JSON Response Read
    instream = entity.getContent();
    result= convertStreamToString(instream);


    jsonArray= new JSONArray(result);

    // json=new JSONObject(result);

    instream.close();
    }

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

    return jsonArray;
    // return json;
    }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. El Presidente

    El Presidente Beware The Milky Pirate!
    VIP Member

    Hi Shruti, welcome to AF! :)

    Are you having issues with code? We've an Application Development Section: Application Development - Android Forums (I've moved some of your other threads there). Can you expand a little on what you need/want please?
     
Loading...
Similar Threads - connectivity through json
  1. centricloon
    Replies:
    9
    Views:
    321
  2. Port_Orchard_mike
    Replies:
    4
    Views:
    512
  3. gokcn
    Replies:
    6
    Views:
    429
  4. mlgch1
    Replies:
    8
    Views:
    508
  5. melani78
    Replies:
    4
    Views:
    706
  6. Ron Miel
    Replies:
    10
    Views:
    1,044
  7. Sebastian huth
    Replies:
    0
    Views:
    477
  8. Viridel
    Replies:
    8
    Views:
    720
  9. ArtScott
    Replies:
    6
    Views:
    1,522
  10. HerrimanCoder
    Replies:
    3
    Views:
    1,133

Share This Page

Loading...