我正在使用 JSON 测试 android 到 PHP 接口和 PHP 到 android 回复系统。它从 Android 应用程序联系我的 PHP 页面并按原样在我的数据库中创建一个条目,但从 PHP 到 android 的回复充满了垃圾。我想知道问题出在哪里:
//PHP
<?php
require_once('functions.php');
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
execute("insert into android_test (USERNAME,FULLNAME) values ('".$obj->{'UserName'}."', '".$obj->{'FullName'}."')"); //android_test (ID,IDUSERS,USERNAME,FULLNAME)
execute("commit");
$posts = array($json);
//$posts = array(1);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
安卓应用:
public void clickbuttonRecieve(View v) { //send to server
try {
JSONObject json = new JSONObject();
json.put("UserName", "test2");
json.put("FullName", "1234567");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
String url = "https://www.mypage.com/scripts/androidportalinput.php";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request); //get feedback
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream); //get feedback
Log.i("Read from server", result);
Toast.makeText(this, result,
Toast.LENGTH_LONG).show();
}
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
我收到的弹出窗口是“帖子”,但也包含页面 html 代码,因此 json 编码的数据是发送到我的 android 应用程序的完整垃圾。
有解决这个吗?
请帮忙