问题描述
任何人都可以提供一个解决方案上面?
现在,我想要做的就是发送一个JSON请求到服务器(例如:{图片:JPG,颜色:绿色}),有PHP访问数据库,然后从服务器的数据库返回的文件名(然后让机器人下载的文件 - 不是一个问题)
任何人都可以提出首先在Android框架,这将帮助我。 - POST JSON到PHP文件在我的服务器
其次一个PHP脚本,将会把JSON到PHP可读的格式(访问数据库不是一个问题,要么,但我不能让JSON成一个对象,然后匹配与数据库)
感谢
修改
感谢下面,但我不要求这一点懒惰的环节,它是从烦恼中我似乎没有能力送一个JSON字符串,并得到了正确的答案。
因此,让我表现出一定的code和找出原因是什么,我认为应该发生的没有发生:
GET URL(使用GET这样我就可以显示工作)
http://example.com/process/json.php?service=GOOGLE
< PHP
//德code JSON字符串到PHP对象
$日codeD = json_de code($ _ GET ['JSON']);
//我不知道它的下面应该工作,但我已经用尽全力。
$为myService = $日codeD->服务; // $服务应该等于:谷歌
$为myService = $日codeD-> {'服务'}; // $服务应该等于:谷歌
//但结果始终是$为myService = NULL - 为什么?
?>
OK,我已经得到了PHP。下面的检索过帐数据,并返回服务
< PHP
$数据=的file_get_contents('php的://输入');
$ JSON = json_de code($的数据);
$服务= $ json-> {'服务'};
打印$服务;
?>
和Android的code:
在的onCreate()
路径=http://example.com/process/json.php;
HttpClient的客户端=新DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000); // 时间到
// 限制
HTT presponse响应;
JSONObject的JSON =新的JSONObject();
尝试 {
HttpPost后=新HttpPost(路径);
json.put(服务,谷歌);
Log.i(杰森对象,json.toString());
post.setHeader(JSON,json.toString());
StringEntity本身=新StringEntity(json.toString());
se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,
应用/ JSON));
post.setEntity(SE);
响应= client.execute(后);
/ *检查响应* /
如果(响应!= NULL){
。InputStream的时间= response.getEntity()的getContent(); //获取
//数据
//的
// 实体
字符串= convertStreamToString(中);
Log.i(读取从服务器,一);
}
}赶上(例外五){
e.printStackTrace();
}
和在任何你想
私有静态字符串convertStreamToString(InputStream的是){
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
StringBuilder的SB =新的StringBuilder();
串线= NULL;
尝试 {
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
}赶上(IOException异常E){
e.printStackTrace();
} 最后 {
尝试 {
is.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
返回sb.toString();
}
Can anybody offer a solution to the above?
For now, all i want to do is send a JSON request to my server (for example: {picture:jpg, color:green}), have the PHP access the database and then return a filename from the server's database (then get android to download the file - not an issue)
Can anybody suggest firstly an Android Framework that will help me with this. - POST JSON to a php file on my server
And secondly a php script that will turn the JSON into php readable format (accessing the database is not an issue either but i cannot get the JSON into an object to then match with the database)
thanks
EDIT
Thanks for the links below but i'm not asking this out of laziness, it's out of annoyance at my seeming inability to send a JSON string and get out the correct answer.
So let me show some code and find out why what i think should happen isn't happening:
GET URL (Using GET so i can show the working)
http://example.com/process/json.php?service=GOOGLE
<?php
// decode JSON string to PHP object
$decoded = json_decode($_GET['json']);
// I'm not sure of which of the below should work but i've tried both.
$myService = $decoded->service; //$service should equal: GOOGLE
$myService = $decoded->{'service'}; //$service should equal: GOOGLE
// but the result is always $myService = null - why?
?>
OK, i've got the PHP. The below retrieves POST ed data and returns the service
<?php
$data = file_get_contents('php://input');
$json = json_decode($data);
$service = $json->{'service'};
print $service;
?>
and the Android Code:
in onCreate()
path = "http://example.com/process/json.php";
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
// Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(path);
json.put("service", "GOOGLE");
Log.i("jason Object", json.toString());
post.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent(); // Get the
// data in
// the
// entity
String a = convertStreamToString(in);
Log.i("Read from Server", a);
}
} catch (Exception e) {
e.printStackTrace();
}
and where ever you want
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
这篇关于Android的JSON到PHP服务器并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!