aspose file tools*
The moose likes Android and the fly likes Sending a jason object via Post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Sending a jason object via Post" Watch "Sending a jason object via Post" New topic
Author

Sending a jason object via Post

Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Hi,

I nead to send a JSON object via POST object. Can anybody help me with a small example ?

Kind Regards,

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

JSON, not "jason".

From where? The client? Server code?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Sorry for the typo.

I nead to send the JSON object fro the client. I have made a JSON object containing the data that needs to be sent. My client is a android phone and needs to write data to a Web Service (already implemented and working) over a Request object and then read the response. How do you send a JSON object over a request.

Some sample code will be good, as i am new to JSON.

Many thanks for your help.

Kind Regards.

Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

I nead to send the login information in the format :{"Email":"abc@xyz.co","Password":"123456"} and then read the user login information back via JSON. Really stuck on this. This is what i am trying. This code is from my android client.


The server reads the data from a variable called jsonRequest. That's why the line tagged with 1 will sets the header variable and its value. responseStr in the last line should hold the value of the user information from the server in the form of a JSON object. I can parse the object but its not working. Where am i going wrong. Really stuck in this, so any help would be appreciated.



Kind Regards,



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
What does "it's not working" mean? Is the HTTP request being sent as you want it to be sent? Is the server receiving all data correctly? If so, does it return all data as it should? And finally, what is the response received on the device?

Also, if you want to send a POST, why are you using HttpGet?

(As an aside, and as I told you before in the other thread where you asked about this, there's nothing at all Android-specific about this.)


Ping & DNS - my free Android networking tools app
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

when i say its not working, i mean i am gettting an invalid request. I have changed the code to the following


The variable a should contain a jason object containing the user information but it contains something like this

<TITLE>ERROR: The requested URL could not be retrieved</TITLE>


The server url is fine and in working condition. My android app can contact to the internet, so that's not fine. Sop where am i going wrong.

Kind Regards,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
I'd start by examining what the server receives; obviously that's different from what it expects to receive.
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

The document given to me states that

Request:{"Email":"abc@xyz.com","Password":"123456"}
The above structure is sent to Server in a variable named “json”


If the request is successful a JSON Object is returned with the user details. If i provide the actual url for the web service could you try and send a request and see what you recieve at your end.

Kind Regards,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Are you saying that the server is not under your control, so that you can't debug on it?
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Yes the server is not under my control. The existing IPhone application is working fine and i am responsible for porting the application to Android.
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

AppURL/users/getuserdetails?jason={"Email":"abc@gmaill.com","Password":"xyz"}

I posted this address in firefox and it printed {"error","-1"}. that was expected. So the web server is working fine. So whats then wrong with this code



All really that i have to do is send a variable called json with the jason object in it. I think things should be much clearer .

Kind Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
There are several differences between using the URL, and using the code you posted:

1) a browser URL causes a GET, not a POST

2) the URL uses "jason" as name of the parameter, whereas the code uses "json" as header name

3) HTTP headers and HTTP parameters are not at all the same thing - if the server expects one then you can't substitute the other
 
 
subject: Sending a jason object via Post