0

I want to send a json message over http to a php server.I used the gson library as you can see.

Gson gson = new Gson();
String[] data = {"value1", "value2", "value3"};
String json = gson.toJson(data);
String message = "jdata"+json; //I did this because of the server implementation 
String path= "http://localhost/joomla/index.php?option=com_up1";

I want to connect to send (POST) the string message to the server that is located on the path The server will retrieve the values, value1,value2,value3 from the message.

$jd = json_decode(JRequest::getVar( 'jdata'), true);
if (sizeof($jd)>0) {


$name = $jd[0];
$surname = $jd[1];
......
 ......

The server will return messages like

if ($db->query()) {
                printf("OK");

that I want to display in my application.

How can I send the message to the server ? And how can I read the messages from the server to my app ?

malcolm the4
  • 307
  • 2
  • 5
  • 15

3 Answers3

0

Have a look at the HttpPost class. A Google search will show you tons of examples.

SimonSays
  • 10,366
  • 5
  • 38
  • 49
  • I have found many examples but none of them explains the steps that are taken to complete HTTP POST.... I could not understand how it works – malcolm the4 May 07 '13 at 22:41
  • Refine the google search to "android httppost json". One of the first results is this one, it does exactly what you are looking for. http://stackoverflow.com/questions/6218143/android-post-json-using-http – SimonSays May 07 '13 at 23:41
0

You say you want it in the path but:

//If it's a parameter it would have to be "jdata="+json
String message = "jdata"+json; 
//And you didn't append it to the path either...
String path= "http://localhost/joomla/index.php?option=com_up1"; 

However, you should really be sending this in the message body.

dmon
  • 29,684
  • 6
  • 84
  • 92
-3

In android long operation such as internet interaction should (in latest versions must) be done in separate thread.

You can accomplish this task in many ways, but i think the simplest consists in creating a subclass of AsyncTask and put the network interaction into the doInBackground method. To interact with the server you can use either the Apache HttpClient or the HttpURLConnection.

user2340612
  • 8,484
  • 4
  • 35
  • 61