2012-04-04 9 views
0

私のPHPスクリプトにいくつかのSQLテーブルクエリを返そうとしています。それが今立っているとしてここに私のスクリプトは次のとおりです。私のPHPが間違った情報を返していますか?

public JSONObject getQuestionsJSONFromUrl(String url, List<NameValuePair> params) { 

    // Making HTTP request 
try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      Log.v("while", line); 
      sb.append(line + "\n"); 
      //Log.v("err", line); 
     } 
     is.close(); 

そしてgetQuestionsJSONを呼び出す方法...:

private static String question_tag = "question"; 
public JSONObject getQuestions(String category) { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("tag", question_tag)); 
    params.add(new BasicNameValuePair("category", category)); 
    //JSONObject json; 
    JSONObject questionsList = jsonParser.getQuestionsJSONFromUrl(questionURL, params); 
    //return json 
    return null; 
} 

<?php 

define("DB_HOST", "localhost"); 
define("DB_USER", "*"); 
define("DB_PASSWORD", "*"); 
define("DB_DATABASE", "*"); 

mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
mysql_select_db(DB_DATABASE); 


if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    $tag = $_POST['tag']; 
    echo $tag; 
if ($tag == 'question') { 
    $category = $_POST['category']; 
    $response=mysql_query("select * from QUESTIONS where CATEGORY like '$category'"); 
    return $category; //just doing this, rather than $response to see if it works 
} 
} 
?> 

そして、ここでは、それに関連付けられているAndroidのコードですそして、ここに私がgetQuestionsJSON ...()メソッドで持っているLog.v()のLogCatがあります:

04-04 20:41:58.721: V/while(933): question 

私はgetQuestions()を実行したときに渡されるStringではなく、なぜこれが「質問」を返すのか実際にはわかりません。 PHPファイルで

+0

は$タグをエコー。あなたの「質問」を返します – marcinj

答えて

2

あなたは

echo $tag; 

を持っており、それが要求への応答です。

これはmysqlのレスポンスを返す必要があります:

if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    $tag = $_POST['tag']; 
    if ($tag == 'question') { 
     $category = $_POST['category']; 
     $response=mysql_query("select * from QUESTIONS where CATEGORY like '$category'"); 

     $rows = array(); 
     while($r = mysql_fetch_assoc($response)) { 
      $rows[] = $r; 
     } 
     print json_encode($rows); 
    } 
} 
+0

これは助けましたが、解決しませんでした。ログが返されます。 04-04 21:11:32.791:V/while(1188):リソースID#2。 次のような出力が簡単なクエリで表示されます:{"category": "elections"、 "id": "0"、 "title": "11月の大統領選挙で誰があなたに投票しますか? : "2012-04-02"、 "enddate": "2012-04-30"、 "responsetype": "0"} {"カテゴリ": "選挙"、 "id": "2"、 "title" "2012-04-02"、 "enddate": "2012-04-30"、 "responsetype": "1"} – Davek804

+0

私は自分の答えを更新しました。レスポンスはJSONにあります。 –

+0

Jaroslawありがとうございました!これは正しいとマークされ、私はまもなく別の投稿を載せる予定です。もしあなたがもう少し手を貸してくれるのであれば、もう一度正しい返答を得ることができると確信しています:)投稿時にリンクします。 – Davek804

関連する問題