我有一个简单的应用程序,并从服务器获取json格式的数据,通过StringBuilder和其他工具将我的json设置为string.my问题是它在listview.it show 12的每一行显示一个奇怪的数字, 6,3,3,6.my json代码生成正确,以下是我的代码:
提供的myjSONcode:
{
"cat": [
{
"id": "5",
"name": "laptop",
"amount": "12"
},
{
"id": "3",
"name": "book",
"amount": "6"
},
{
"id": "1",
"name": "computer",
"amount": "3"
},
{
"id": "2",
"name": "game",
"amount": "3"
},
{
"id": "4",
"name": "travel",
"amount": "6"
}
],
"success": 1
类CatParser:这个类获取json代码并解析它以提供来自json的set exracted信息。
public class CatParser {
private static String Log_Label = "error";
private static List<HashMap<String, Object>> list_all;
public static List<HashMap<String, Object>> parseJson(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jArray = jsonObject.getJSONArray("cat");
list_all = new ArrayList<>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject temp = jArray.getJSONObject(i);
HashMap<String, Object> cats = new HashMap<>();
cats.put("id", temp.getString("id"));
cats.put("name ", temp.getString("name"));
cats.put("amount", temp.getString("amount"));
list_all.add(cats);
}
} catch (Exception e) {
Log.i(Log_Label , "error in Catparser : " + e.toString());
}
return list_all;
}
JSONdl类:这个类连接到生成json代码的php网页,并以字符串格式获取json代码。
public class JSONdl {
private static String Log_Label = "error";
public static String data = "";
public static String JsonIntoString(final String StrlUrl) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(StrlUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
data = stringBuilder.toString();
} catch (Exception e) {
Log.i(Log_Label, "error in JSONdl" + e.toString());
}
}
});
thread.start();
return data;
}
和MainActivity.java:在这个类中我想在listview上设置一个simpleAdapter,但我的listview显示一个奇怪的东西,它在列表视图中显示12,6,3,3,6,我无法实现。
public class MainActivity extends AppCompatActivity {
private String url = "http://172.29.41.174/get_cat.php";
private static String Log_Label = "error";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAdapter();
}
private void setAdapter() {
try {
String json = JSONdl.JsonIntoString(url);
List<HashMap<String, Object>> objects = CatParser.parseJson(json);
String[] from = {String.valueOf(R.mipmap.ic_launcher), "name", "amount"};
int[] to = {R.id.img, R.id.name, R.id.tedad};
SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(), objects, R.layout.row, from, to);
ListView lst = (ListView) findViewById(R.id.lst);
lst.setAdapter(simpleAdapter);
} catch (Exception e) {
Log.i(Log_Label, "error in recieveJson() in Mainactivity: " + e.toString());
}
}