0

我想通过 http 向 php 服务器发送一条 json 消息。如您所见,我使用了 gson 库。

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";

我想连接以将字符串消息发送(POST)到位于路径上 的服务器服务器将从消息中检索值 value1、value2、value3

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


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

服务器将返回类似的消息

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

我想在我的应用程序中显示。

如何将消息发送到服务器?以及如何将消息从服​​务器读取到我的应用程序?

4

3 回答 3

0

你说你想要它在路径中但是:

//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"; 

但是,您确实应该在消息正文中发送它。

于 2013-05-07T21:46:29.900 回答
0

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

于 2013-05-07T21:39:51.843 回答
-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.

于 2013-05-07T21:38:46.740 回答