【发布时间】:2017-01-08 16:08:31
【问题描述】:
我从mysql database 到php 获取数据,我获取了两个值,但适配器只显示一个。
这是我的 android getData() 函数,现在正在获取一列我想获取第二列,但我没有得到
public void getdata(){
try {
URL url=new URL(retri_url);
try {
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
IS=new BufferedInputStream(httpURLConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(IS));
StringBuilder stringBuilder=new StringBuilder();
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line+"\n");
}
IS.close();
result=stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
try{
JSONArray jsonArray=new JSONArray(result);
JSONObject jsonObject=null;
data=new String[jsonArray.length()];
for(int i=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
int id=Integer.parseInt(jsonObject.get("id").toString());
String strI = String.valueOf(id);
data[i] =strI;
data[i]=jsonObject.getString("Name");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
因为你只在字符串数组中获取一列
Address,所以使用 POJO 类 -
你能举个例子吗
-
@PavneetSingh 我已经编辑了代码请检查只有一列正在获取.. 并非所有列都不起作用!
-
发表您的回复
-
我只得到一列,但日志猫没有错误