This question already has answers here:
Populate Listview from JSON
(5个答案)
6年前关闭。
嗨,我是一个新程序员,我正在尝试将这个输出解析到我的android列表视图中,我使用了连接到mysql的php来生成这个输出:
我知道关于同一个话题有很多问题,但我似乎找不到答案!如果这是个简单的问题,真的很抱歉。提前谢谢!
这是我的java代码,它当前将我的输出显示在一个文本视图中。但只有第一个用户。
我的PHP代码。
公众党
(5个答案)
6年前关闭。
嗨,我是一个新程序员,我正在尝试将这个输出解析到我的android列表视图中,我使用了连接到mysql的php来生成这个输出:
{"id":"2","name":"Username : garrett","password":"Password : important"}{"id":"1","name":"Username : darrel","password":"Password : pass1234"}
我知道关于同一个话题有很多问题,但我似乎找不到答案!如果这是个简单的问题,真的很抱歉。提前谢谢!
这是我的java代码,它当前将我的输出显示在一个文本视图中。但只有第一个用户。
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();
}
?>
最佳答案
菲律宾比索
$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();
}
公众党
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);
}
10-07 16:22