Java 您好,我正在尝试将json数据从url显示到listview中。不过什么也没有显示
网址 私有静态字符串JSON_URL=”https://run.mocky.io/v3/aff3f637-d04f-45c0-b002-09be1a143784"; 显示数据Java 您好,我正在尝试将json数据从url显示到listview中。不过什么也没有显示,java,android,json,Java,Android,Json,网址 私有静态字符串JSON_URL=”https://run.mocky.io/v3/aff3f637-d04f-45c0-b002-09be1a143784"; 显示数据 @Override protected void onPostExecute(String s) { try{ JSONObject jsonObject= new JSONObject(s); JSONArray jsonArray = js
@Override
protected void onPostExecute(String s) {
try{
JSONObject jsonObject= new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("Friends"); //name of array
for(int i = 0; i <jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
namey=jsonObject.getString("name");
age= jsonObject1.getString("age");
//Hashmap
HashMap<String, String> friends = new HashMap<>();
friends.put("name",namey);
friends.put("age", age);
friendsList.add(friends);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Displaying the results
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
friendsList,
R.layout.row_layout,
new String[]{"name","age"},
new int[]{R.id.textView, R.id.textView2});
lv.setAdapter(adapter);
}
}
@覆盖
受保护的void onPostExecute(字符串s){
试一试{
JSONObject JSONObject=新的JSONObject;
JSONArray JSONArray=jsonObject.getJSONArray(“朋友”);//数组名称
对于(int i=0;i
您的mocky JSON格式错误,您需要删除最后一个字符字符串s
是否检查了变量s的值?Toast()是否查看。记录它以了解情况。它是否为空()?您没有检查空()。如果在doInbackground()中捕获到异常,它将为空;浏览器中的url:SyntaxError:JSON.parse:JSON数据第26行第1列的JSON数据后出现意外的非空白字符
catch(JSONException e){e.printStackTrace();}
至少可以显示Toast()你有没有在日志中查看是否有问题?嗨,阿塔纳斯-发帖时请注意格式。另外,请将你的标题切中要害。我试图编辑以修复代码格式,但无法修复,因为这是一个警告,问题几乎都是代码。最好按照pos前评论中的建议进行基本检查另外,你可以在你的帖子中说你已经尝试过做什么来解决问题:)
@Override
protected String doInBackground(String... strings) {
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(JSON_URL);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
int data = isr.read();
while (data != -1) {
current += (char) data;
data = isr.read();
}
return current;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
} catch (Exception e){
e.printStackTrace();
}
return current;
}
@Override
protected void onPostExecute(String s) {
try{
JSONObject jsonObject= new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("Friends"); //name of array
for(int i = 0; i <jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
namey=jsonObject.getString("name");
age= jsonObject1.getString("age");
//Hashmap
HashMap<String, String> friends = new HashMap<>();
friends.put("name",namey);
friends.put("age", age);
friendsList.add(friends);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Displaying the results
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
friendsList,
R.layout.row_layout,
new String[]{"name","age"},
new int[]{R.id.textView, R.id.textView2});
lv.setAdapter(adapter);
}
}