嗨,我正在将测试安卓应用程序链接到 MySQL 数据库,来自.php文件中的 JSON 回显。
我能够用整个数据填充 ArrayList,但现在我想将数据分成变量,但我真的找不到方法。
我正在使用loopJ库进行JSON通信
我的主要活动.java 公共类 MainActivity 扩展 AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.lvLista);
getData();
}
public void getData()
{
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://www.kukitosoft.com/android/api.php";
// RequestParams params = new RequestParams();
// params.put("campo", "valor a fitrar");
//Esto iria en el null del client.post
client.post(url, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200)
{
loadList(getDataJSON(new String(responseBody)));
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
public void loadList(ArrayList<String> data)
{
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
list.setAdapter(adapter);
}
public ArrayList<String> getDataJSON(String response)
{
ArrayList<String> list = new ArrayList<String>();
try
{
JSONArray jsonArray = new JSONArray(response);
String texto;
for(int i=0; i<jsonArray.length(); i++)
{
texto = jsonArray.getJSONObject(i).getString("id") + " " +
jsonArray.getJSONObject(i).getString("nombre") + " " +
jsonArray.getJSONObject(i).getString("descripcion") + " " +
jsonArray.getJSONObject(i).getString("calificacion") + " ";
list.add(texto);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
}
首先,为必填字段创建一个具有不同 getter/setter 的 pojo 类。
public class MyResponse(){
private List<String> idList;
private List<String> nombreList;
private List<String> descList;
private List<String> califList;
public void setIdList(List<String> list){
this.idList = list;
}
public void getIdList(){
return idList;
}
public void setNombreList(List<String> list){
this.nombreList = list;
}
public void getNombreList(){
return nombreList;
}
public void setDescList(List<String> list){
this.descList = list;
}
public void getDescList(){
return descList;
}
public void setCalifList(List<String> list){
this.califList = list;
}
public void getCalifList(){
return califList;
}
}
现在解析每个响应并添加到MyResponse类型的列表中
public MyResponse getDataJSON(String response)
{
MyResponse myResponse = new MyResponse();
ArrayList<String> idList = new ArrayList<String>();
ArrayList<String> nombreList = new ArrayList<String>();
ArrayList<String> descList = new ArrayList<String>();
ArrayList<String> califList = new ArrayList<String>();
try
{
JSONArray jsonArray = new JSONArray(response);
for(int i=0; i<jsonArray.length(); i++)
{
idList.add(jsonArray.getJSONObject(i).getString("id"));
nombreList.add(jsonArray.getJSONObject(i).getString("nombre"));
descList.add(jsonArray.getJSONObject(i).getString("descripcion"));
califList.add(jsonArray.getJSONObject(i).getString("calificacion"));
}
myResponse.setIdList(idList);
myResponse.setNombreList(nombreList);
myResponse.setDescList(descList);
myResponse.setCalifList(califList);
return myResponse;
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
要从getDataJSON(( 检索数据
MyResponse myResponse = getDataJSON(new String(responseBody)));
ArrayList<String> idList = myResponse.getIdList();
ArrayList<String> nombreList = myResponse.getNombreList();
ArrayList<String> descList = myResponse.getDescList();
ArrayList<String> califList = myResponse.getCalifList();