我需要将一些数据从我的 android 应用程序传递到我的 PHP 服务器。
这是我的代码
public class BackgroundDataLoader extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
JSONObject jsObj=new JSONObject();
try {
jsObj.put("ID", 1);
jsObj.put("Name", "Shashika");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url+"/data.php");
try {
StringEntity se=new StringEntity(jsObj.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httppost.setEntity(se);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONArray finalResult = null;
String json = null;
HttpResponse response = null;
String text = null;
JSONArray jsonArray;
JSONObject jsonObject = null;
// Execute HTTP Post Request
try {
response = httpclient.execute(httppost);
int statusCode=response.getStatusLine().getStatusCode();
if(statusCode==200){
HttpEntity entity=response.getEntity();
text=EntityUtils.toString(entity);
}
else{
return "error "+response.getStatusLine().getStatusCode();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jsonArray= new JSONArray(text);
//text=jsonArray.getJSONObject(0).getJSONArray(name);
text=jsonArray.getString(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String tag=text;
Log.d(tag, text);
return text;
}
现在我需要在我的服务器的 php 文件中将这些数据作为 JSON 对象读取。如何在服务器的 php 文件中读取这些数据?