! 私は、PHP Web サービスを使用して Android クイズのデータを作成/ホスト/維持するクイズアプリに取り組んでいます。
問題の 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;
}
次に、このように使用して機能を実行しています。/// アプリの他のセクションからコードを渡します 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 に対して返される内容は次のとおりです。これを処理するにはどうすればよいですか?