-2
URL을 하드 코드하여 ReadJSON()으로 만들면 응용 프로그램이 작동합니다. execute (내 URL);이미지가 JSON 파일에서 목록보기로로드되지 않습니다.
그러나 url을 textinput으로 변경하면 이미지가로드되지 않습니다.
이 예상 된 결과입니다 : 여기
https://imgur.com/a/QyKG1 전체 파일입니다
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();
}
}
StackOverflow는 외부 링크 대신 게시물 자체에 코드를 넣는 것을 선호합니다. 앞으로는 외부 링크가 작동하지 않을 수 있으므로 다른 사람들에게 질문이 불분명하게됩니다. –