! 我正在开发一个测验应用程序,它使用PHP Web服务为Android quizes创作/托管/维护数据。
Heres是有问题的PHP函数。
我正在寻找PHP代码中的帖子。
if (isset($_POST['verifyCourse'])){
verifyCourse($_POST['courseCode']);}
然后这指向函数......
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;
}
然后我就像这样使用它来执行功能。 ///从app的其他部分传递代码 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取回的内容,我该如何处理?
答案 0 :(得分:1)
mysql_fetch_assoc
返回一个数组。所以在你的代码中你会得到类似的东西:
echo json_encode(array('Course' => array()));
结果是一个字符串:{"Course":[]}
。在JSON中,这是一个对象。所以你需要用:
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jArray = jsonObject.getJSONArray("Course");
另请参阅:Reading a Json Array in android
<强>更新强>
在你的php中:
function verifyCourse($courseCode){
$result = mysql_query("SELECT * FROM Course WHERE CourseCode = \"$courseCode\" LIMIT 1");
$rows = array();
while ($r = mysql_fetch_assoc($result))
{
$rows[]= $r;
}
header('Content-type: application/json');
return json_encode(array('Course' => $rows));
exit;
}
输出如下字符串:
{"Course":[{"key1":"value1","key2":"value2"},{"2key1":"2value1","2key2":null}]}
你的java代码中的:
JSONObject jsonObject = new JSONObject(builder.toString());
JSONArray jArray = jsonObject.getJSONArray("Course");
for(int i = 0; i < jArray.length(); i++){
JSONObject jObject = jArray.getJSONObject(i);
Iterator<String> keys = jObject.keys();
storage = new HashMap<String, String>();
while( keys.hasNext() ){
String key = (String)keys.next();
storage.put(key, jObject.get(key).toString());
}
results.add(storage);
}