我的Android应用需要将数据发送到PHP服务器。我是PHP新手,不知道如何在服务器上接收JSON和解码对象。如果有人能给我启动,那将是很有帮助的。
JSONObject json = new JSONObject();
try
{
json.put("dog", "cat");
}
catch (JSONException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection urlConnection = null;
try
{
URL url = new URL("http://10.0.2.2/server.php");
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
OutputStreamWriter os = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
os.write(json.toString());
os.close();
}
答案 0 :(得分:0)
一些例子。你的MyActivity课程
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylyout);
new PostToServer().execute();
}
class PostToServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage(getString(R.string.progdata));
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String cat = "mycat";
String dog = "mydog";
HttpParams httpParameters = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParameters);
client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
client.getParams().setParameter("http.socket.timeout", 2000);
client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
HttpPost request = new HttpPost("http://yourserver/your.php);
request.getParams().setParameter("http.socket.timeout", 5000);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("cat", cat));
postParameters.add(new BasicNameValuePair("dog", dog));
try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
String result = sb.toString();
String delimso = "[;]+";
String[] resultxxx = result.split(delimso);
if( resultxxx[0].equals("1")) {
// successfully updated
//to do some
}else {
// unsuccessfully updated
//to do some
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product uupdated
pDialog.dismiss();
}
}
服务器上的your.php脚本
<?php
$cat = $_POST['cat'];
$dog = $_POST['dog'];
//login to database
//mysql operations $resttt
$result = mysql_query("$resttt");
$res="0;0";
if($result) { $res="1;0"; }
echo $res;
?>
答案 1 :(得分:0)
谢谢大家的帮助。我已经处理了一些代码,它正在运行。
PHP服务器脚本
$msg = file_get_contents('php://input');
$input = json_decode($msg,true);
现在,此时json对象存储在$ input变量中。