我想通过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");
我希望在我的应用程序中显示。
如何将消息发送到服务器? 我如何阅读从服务器到我的应用程序的消息?
答案 0 :(得分:0)
查看HttpPost课程。 Google search将显示大量示例。
答案 1 :(得分: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";
但是,你应该在邮件正文中发送它。
答案 2 :(得分:-3)
在Android长时间操作中,例如互联网交互应该(在最新版本中必须)在单独的线程中完成。
您可以通过多种方式完成此任务,但我认为最简单的方法是创建AsyncTask的子类并将网络交互放入doInBackground
方法。要与服务器进行交互,您可以使用Apache HttpClient或HttpURLConnection。