0

mysqlデータベースに接続してそこから情報を取得する必要があるAndroidアプリケーションを開発しています。phpスクリプトを使用してサーバーに接続し、クエリを実行して、結果をjson形式で出力しています。

if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass) || !mysql_select_db($mysql_db)){
die($conn_error);
}



$q=mysql_query("SELECT * FROM food");

  while($e=mysql_fetch_assoc($q))

          $output[]=$e;

       print(json_encode($output));

mysql_close();




?>

これは、HttpGetを使用してデータを取得し、それを文字列に変換するJavaコードです。次に、データを解析して画面に表示することを目的としています。

public class HttpExample extends Activity {

TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    httpStuff = (TextView) findViewById(R.id.tvHttp);

    GetMethodEx test = new GetMethodEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class GetMethodEx {

    public String getInternetData() throws Exception {

        BufferedReader in = null;
        String data = "";
        String returnString = null;

        // httpGet

        try {

            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://10.0.2.2/connect.php");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");

            while ((l = in.readLine()) != null) {
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            // return data;
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        // parse json data
        try {
            JSONArray jArray = new JSONArray(data);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag", "Foodname: " + json_data.getString("food"));
                // Get an output to the screen
                returnString += "\n\t" + jArray.getJSONObject(i);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return returnString;

    }

}

}

データを解析することを除いて、私がやりたいことはすべてやっています。食べ物の名前だけが欲しいときは、データベースの内容全体が画面に表示されているだけです。誰か助けてもらえますか?ありがとうございます。

編集:これは私のphpスクリプトからの私のjson出力のスクリーンショットです

ここに画像の説明を入力してください

4

1 に答える 1

2

試す

returnString += "Foodname: " + json_data.getString("food") + "\n";

それ以外の

returnString += "\n\t" + jArray.getJSONObject(i);

これは食べ物の名前だけが表示されると思います。私の携帯電話でこれを書いているので、私は今それをテストすることができません。

于 2012-08-15T14:08:33.790 に答える