我有一个问题,我的值显示我想要回声 我知道基于log cat我正在发布的内容正在收到,因为下面的代码返回 我从单个基本值对中发布的值。
public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {
String url = "http://mydomain.com/map/";
@Override
protected JSONObject doInBackground(JSONObject... data) {
JSONObject json = data[0];
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
JSONObject jsonResponse = null;
HttpPost post = new HttpPost(url);
try {
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=new JSONObject(resFromServer);
Log.i("Response from server", jsonResponse.getString("msg"));
} catch (Exception e) { e.printStackTrace();}
return jsonResponse;
}
}在我的mainActivity中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
HashMap user = new HashMap();
user = db.getUserDetails();
try {
JSONObject toSend = new JSONObject();
toSend.put("msg",user.get("email").toString());
JSONTransmitter transmitter = new JSONTransmitter();
transmitter.execute(new JSONObject[] {toSend});
} catch (JSONException e) {
e.printStackTrace();
}
map.php的地图
if( isset($_POST["json"]) ) {
$data = json_decode($_POST["json"]);
$data->msg = strrev($data->msg);
echo json_encode($data);
}
?>
我正在关注的教程可以在下面的网址找到 http://mongskiewl.wordpress.com/2013/10/16/sending-json-data-from-android-to-a-php-script/