Tutorial: Simple HttpClient + JSON object exchange

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Tutorial: Simple HttpClient + JSON object exchange

Postby zoniq » Sat Nov 07, 2009 4:38 pm

zoniq
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Sep 28, 2009 12:33 pm

Top

Postby fireshaper » Wed Dec 09, 2009 7:10 pm

zoniq: Could you tell me the easiest way to execute an http request and receive the JSON reply? Thanks!
Rory
fireshaper
Freshman
Freshman
 
Posts: 3
Joined: Wed Dec 09, 2009 7:07 pm

Postby zoniq » Wed Dec 09, 2009 7:19 pm

I thought I did so in my tutorial ;-) Or what do you mean exactly?
zoniq
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Sep 28, 2009 12:33 pm

Postby fireshaper » Wed Dec 09, 2009 7:25 pm

Currently I'm sending the request via

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. responseHandler = new BasicResponseHandler();
  2. System.out.println(httpclient.execute(httpget, responseHandler));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


And I want to see what the return JSON code is. I'm using Eclipse, FYI.
Rory
fireshaper
Freshman
Freshman
 
Posts: 3
Joined: Wed Dec 09, 2009 7:07 pm

Postby zoniq » Wed Dec 09, 2009 7:28 pm

In the tutorial I'm using:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Raw DEBUG output of our received JSON object:
  2.  
  3.  Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This prints the elements of the received JSONObject into your Logcat output.
zoniq
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Sep 28, 2009 12:33 pm

Postby fireshaper » Wed Dec 09, 2009 7:44 pm

Could I do something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ResponseHandler responseHandler = new BasicResponseHandler();
  2.  
  3.         jsonObjRecv = (JSONObject) httpclient.execute(httpget, responseHandler);
  4.  
  5.         Log.v(TAG, "<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Rory
fireshaper
Freshman
Freshman
 
Posts: 3
Joined: Wed Dec 09, 2009 7:07 pm

Top

Postby padde » Thu Dec 10, 2009 8:51 am

The BasicResponseHandler returns the results as a string so something like this should work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     ResponseHandler responseHandler = new BasicResponseHandler();
  3.  
  4.     JSONObject jsonObjRecv = JSONObject(httpclient.execute(httpget, responseHandler));
  5.  
  6.     Log.v(TAG, "<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
  7.  
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


But you could write your own JSONObjectResponseHandler that returns a JSONObject.
I guess even the cast to JSONObject would be necessary then.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby binnyb » Sun May 16, 2010 7:36 pm

After putting together a project including this code, I found that it only loads one object into the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. JSONObject jsonObjRecv = new JSONObject(resultString);
  2.  
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
JSONObject. It leaves out all the other data that is important to retrieve. I notice that in the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String resultString= convertStreamToString(instream);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
String, all of the data is available. Why is all of the data left out after the first sequence of { } in my json file?

Edit: i just fixed this problem, stupid mistake.

Resolution: instead of returning a JSONObject, i return a JSONArray.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String resultString = convertStreamToString(inputStream);
  3.  
  4. inputStream.close();
  5.  
  6. JSONArray jsonArrayReceived = new JSONArray(resultString);
  7.  
  8. return jsonArrayReceived;
  9.  
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
binnyb
Freshman
Freshman
 
Posts: 2
Joined: Sun May 16, 2010 4:41 pm

Re: Tutorial: Simple HttpClient + JSON object exchange

Postby erica » Thu Nov 25, 2010 10:15 am

Can someone please tell me if the url is to a PHP page, how do i read the value of 'key_1' and 'key_2' used in this tutorial? Sorry to ask this simple question but i have no idea how to get the values. Thanks in advance.
erica
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 25, 2010 10:04 am

Re: Tutorial: Simple HttpClient + JSON object exchange

Postby daj » Sat Apr 09, 2011 12:32 pm

@erica:

Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. <?php
  2. $reqBodyJSON = @file_get_contents('php://input');
  3. $reqObj = json_decode($reqBodyJSON);
  4. $email =  $reqObj->{'email_key'};
  5. $password = $reqObj->{'password_key'};
  6. $id = $reqObj->{'id_key'};
  7. ?>
  8.  
Parsed in 0.066 seconds, using GeSHi 1.0.8.4
daj
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 09, 2011 12:26 pm

Re: Tutorial: Simple HttpClient + JSON object exchange

Postby daj » Sat Apr 09, 2011 12:35 pm

does anyone know how to send over SSL to a self-signed cert?
daj
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 09, 2011 12:26 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 5 guests