我有一个类执行 POST 到 php 页面,该页面应该对发布的数据进行一些操作,并返回结果(类似于 OK,KO)。如何在 PHP 中将结果发送回,以及如何在 android 中使用它?
在Android方面,我有这个,它发布数据:
public int postData(String id, String webrispid, String userid) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.server.com/postpage.php");
int responseCode = 0;
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("user", userid));
nameValuePairs.add(new BasicNameValuePair("webrisid", webrispid));
try {
nameValuePairs.add(new BasicNameValuePair("token",AeSimpleSHA1.SHA1(userid) ));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseCode = response.getStatusLine().getStatusCode();
System.out.println(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return responseCode;
}
实际上 postpage.php 看起来像这样:
<?php
if ( isset($_POST['user']) ) {
// try to store the data posted
//if data is stored return "ok"
// else return "ko"
} else {
// error
}
?>
所以在android端,我应该停止发送相同的数据并开始处理其余数据并最终将其发送到页面......