1

嗨,我是一名新程序员,我正在尝试将此输出解析到我的 android listview 中,我使用连接到 mysql 的 php 来生成此输出:

{"id":"2","name":"Username : garrett","password":"Password : important"}{"id":"1","name":"Username : darrel","password":"Password : pass1234"}

我知道有很多关于同一主题的问题,但我似乎无法找到答案!如果这是一个简单的问题,真的很抱歉。提前致谢!

这是我的 java 代码,它当前将我的输出显示到 textView 中。但只有第一个用户。

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;


@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class Users extends Activity {
/** Called when the activity is first created. */

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://172.30.54.153/databases/");
    TextView textView = (TextView)findViewById(R.id.textView1);
    TextView textView2 = (TextView)findViewById(R.id.textView2);

  try {

   HttpResponse response = httpclient.execute(httppost);
   String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
   JSONObject object = new JSONObject(jsonResult);

   String name = object.getString("name");
  String password = object.getString("password");
  textView.setText(name + " - " + password);

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


   }
private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    try {
     while ((rLine = rd.readLine()) != null) {
      answer.append(rLine);
       }
    }

    catch (IOException e) {
        e.printStackTrace();
     }
    return answer;
   }
}

我的 PHP 代码。

 <?php 


 require 'connect.inc.php';

 $query = "SELECT `id` , `username`, `password` FROM `users` ORDER BY `id`";

 if ($query_run = mysql_query($query)) {

 while($query_row = mysql_fetch_assoc($query_run)) {
 $username = $query_row['username'];
 $password = $query_row ['password'];
 $id = $query_row ['id'];
 //echo $username .'`s password is : '. $password.'<br>';
 $data = array('id'=>$id , 'name'=> 'Username : '. $username ,'password'=>'Password : '.$password);
 print(json_encode($data));
 //print(json_encode($password));
 }


 }else{
 echo mysql_error();
 }

 ?>
4

2 回答 2

1

php

$final_data = array();    
if ($query_run = mysql_query($query)) 
    { 
        $i=0;
        while($query_row = mysql_fetch_assoc($query_run)) 
        { 
            $username = $query_row['username']; 
            $password = $query_row ['password']; 
            $id = $query_row ['id']; 
            //echo $username .'`s password is : '. $password.'<br>'; 
            $data = array('id'=>$id , 'name'=> 'Username : '. $username ,'password'=>'Password : '. $password); 
            $final[$i]=$data;
            $i++;
            //print(json_encode($data)); 
            //print(json_encode($password)); 
        } 
        print(json_encode($final_data));
    }else{
     echo mysql_error(); 
    }

java部分

String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONArray mArray = new JSONArray(jsonResult);
for (int i = 0; i < mArray.length(); i++) {
    JSONObject object = mArray.getJSONObject(i);

    String name = object.getString("name");
    String password = object.getString("password");
    textView.setText(name + " - " + password);
}
于 2013-03-22T06:22:08.713 回答
1

从您的服务器返回一个 json 对象数组。改变这个

JSONObject object = new JSONObject(jsonResult);

对此

JSONArray objects = new JSONArray(jsonResult);

然后迭代 jsonArray 并为每个数组项构建一个 jsonObject

for (int i = 0; i < objects.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);

    // do something here

}
于 2013-03-22T01:55:09.890 回答