! 我正在开发一个测验应用程序,它使用 PHP Web 服务来创作/托管/维护 Android 测验的数据。
这是有问题的PHP函数。
我正在寻找我的 PHP 代码中的帖子。
if (isset($_POST['verifyCourse'])){
verifyCourse($_POST['courseCode']);}
任何那么 this 指向函数...
function verifyCourse($courseCode){
$result = mysql_query("SELECT * FROM Course WHERE CourseCode = \"$courseCode\";");
$rows = array();
while ($r = mysql_fetch_assoc($result)) {
$rows = $r;
}
if($rows == null){
return 0;
} else {
return json_encode(array('Course' => $rows));
}
}
然后在我的 Android 代码上,我这样做是为了向名为“verifyCourse”的服务器发送一个 POST,但我没有得到任何回报。
Android:发送 HTTP POSTS 的函数
public ArrayList<HashMap<String, String>> httpPost(List<NameValuePair> valuepair, String code)
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mydomain.edu/quiz-app/webservice.php");
StringBuilder builder = new StringBuilder();
try {
httppost.setEntity(new UrlEncodedFormEntity(valuepair));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
/* Checking response */
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
Log.d("myapp", "response " + response.getEntity());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}else{
Log.e("POST-return", "Failed to download file");
}
} catch (Exception e) {
}
ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
HashMap<String, String> storage = new HashMap<String, String>();
String value;
try{
JSONArray jArray = new JSONArray(builder.toString());
for(int i=0; i<jArray.length(); i++){
JSONObject jsonObject = jArray.getJSONObject(i);
value=jsonObject.getString(code);
storage = new HashMap<String, String>();
storage.put(code, value);
results.add(storage);
}
} catch (Exception e) {
}
return results;
}
然后我像这样使用它来执行功能。/// 从应用程序的其他部分传递代码 public void getCourseCodesandVerify(String code) {
List<NameValuePair> course_info = new ArrayList<NameValuePair>(2);
course_info.add(new BasicNameValuePair("verifyCourse",null));
course_info.add(new BasicNameValuePair("courseCode",code));
httpPost(course_info,null);
}
知道为什么我的代码什么都不返回...?
这是我返回的 JSON,我该如何处理?