嗨,我是一名新程序员,我正在尝试将此输出解析到我的 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();
}
?>