2011-07-13 11 views
0

私はhereというコードで遊んでいました。私はnullのjsonオブジェクトを返すようにデータベースを取得することしかできません。ここに私のコードです。php webserviceは常にnullの値を返しています

public class TableTalkSplash extends Activity { 
    private InputStream is; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String result = ""; 
     InputStream is = null; 
     //the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("row_id","1")); 

     //http post 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://mywebsite.com/myscript.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 
     //convert response to string 
     try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 

       result=sb.toString(); 
       System.out.println(result); // this is printing "null" to the console 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     //parse json data 
     try{ 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         Log.i("log_tag","id: "+json_data.getInt("id")+ 
           ", name: "+json_data.getString("name")+ 
           ", sex: "+json_data.getInt("sex")+ 
           ", birthyear: "+json_data.getInt("birthyear") 
        ); 
       } 

     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); //exception is being caught here 
     } 
    } 
} 

ここは私のPHPスクリプトです。

<?php 
mysql_connect("host","username","password"); 
mysql_select_db("mydb"); 

$q=mysql_query("SELECT * FROM dinner_info WHERE row_id ='".$_REQUEST['row_id']."'"); 
while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
?> 

現在、Webサイトを実行しているApacheウェブサーバーがあり、.phpファイルはindex.htmlファイルと同じディレクトリにあります。私は、apacheサーバーと同じボックスで実行されているmySQLデータベースを持っています。私は実際にデータベースについてほとんど知っていませんが、SELECT * FROM dinner_info WHERE row_id = 1を実行するとわかります。正しい情報が返されます。だから、私はエラーがPHPにあると推測しています。私は "ホスト"をlocalhostと "username"で置き換えようとしましたが、私のユーザ名と "password"はパスワードで置き換えました。何か案は?もっと情報が必要なら私に知らせてください!みんな、ありがとう。

+1

PHPでエラー報告をオンにするには、トップでこれを置く: 'のerror_reportingを(-1);' 。 –

+0

ありがとう、それは私の問題の一部ですが、私はどのようにスクリプト言語をデバッグするかという手がかりがありません。私はそれを追加し、それが何を言うかを見ます。 –

+0

私はそれを<?phpタグの直後にphpスクリプトの先頭に追加し、エラーを報告していません。エラーがある場合は、どこに表示されますか? –

答えて

0

私はDB接続またはクエリは、デバッグを支援するためにこれを試して失敗している推測している:

<?php 

error_reporting(-1); 
ini_set("display_errors", 1); 

mysql_connect("host","username","password") or die('Could not connect: ' . mysql_error());; 
mysql_select_db("mydb"); 

$query = "SELECT * FROM dinner_info WHERE row_id ='".mysql_real_escape_string($_REQUEST['row_id'])."'" 

$q=mysql_query($query); 

if (!$q) { 
    $message = 'Invalid query: ' . mysql_error() . "\n"; 
    $message .= 'Whole query: ' . $query; 
    die($message); 
} 

while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
+0

PHPをデバッグする方法を教えてくれてありがとう!私は自分の問題を解決することができました。 –

関連する問題