图片未从 JSON 文件加载到列表视图
Images not loading from JSON file to Listview
当我将 URL 硬编码到 ReadJSON().execute(我的 url 在这里)时,应用程序工作;
然而,将 url 更改为文本输入后,它不会加载图像。
这是预期的结果:https://imgur.com/a/QyKG1
这是整个文件:https://pastebin.com/e6Q5ViuS
这是包含错误的代码:
class ReadJSON extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
try {
JSONArray jsonArray = new JSONArray(content);
for(int i =0;i<jsonArray.length(); i++){
JSONObject productObject = jsonArray.getJSONObject(i);
arrayList.add(new Product(
productObject.getString("photo"),
productObject.getString("author")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adapter);
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
// create a url object
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
问题出在这里:
txtUrl.getText().toString();
你没有初始化txtUrl
,这意味着Android不知道txtUrl
是什么。您可以将其更改为:
txtUrl = (EditText) findViewById(R.id.youredittextid);
EditTextValue = txtUrl.getText().toString();
当我将 URL 硬编码到 ReadJSON().execute(我的 url 在这里)时,应用程序工作;
然而,将 url 更改为文本输入后,它不会加载图像。
这是预期的结果:https://imgur.com/a/QyKG1
这是整个文件:https://pastebin.com/e6Q5ViuS
这是包含错误的代码:
class ReadJSON extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
try {
JSONArray jsonArray = new JSONArray(content);
for(int i =0;i<jsonArray.length(); i++){
JSONObject productObject = jsonArray.getJSONObject(i);
arrayList.add(new Product(
productObject.getString("photo"),
productObject.getString("author")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adapter);
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
// create a url object
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
问题出在这里:
txtUrl.getText().toString();
你没有初始化txtUrl
,这意味着Android不知道txtUrl
是什么。您可以将其更改为:
txtUrl = (EditText) findViewById(R.id.youredittextid);
EditTextValue = txtUrl.getText().toString();