ArrayList<Product> arrayList;
JSONObject jsonObject, productObject;
JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.*);
arrayList = new ArrayList<>();
ko_gv = (GridView) findViewById(R.id.Ko_gridView);
runOnUiThread(new Runnable() {@Override
public void run() {new ReadJSON().execute("");}
});
}
class ReadJSON extends AsyncTask<String, Integer, String> {@Override
protected String doInBackground(String... params) {
return readURL(params[0]);}
@Override
protected void onPostExecute(String content) {
try {jsonObject = new JSONObject(content);
jsonArray = jsonObject.getJSONArray("products");
for(int i =0;i<jsonArray.length(); i++){
productObject = jsonArray.getJSONObject(i);
arrayList.add(new Product(
productObject.getString("image"),
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList);
ko_gv.setAdapter(adapter);
} }
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
지금 작성한 코드 일부인데요.
디비서버가 따로 있고, 디비서버에서 JSON형식으로 데이터를 불러와서 어댑터로 파싱해줍니다.
여기서 ReadJSON 이랑 readURL 부분이 다른 자바파일에서도 중복이 되서 따로 자바파일로 만들어서 쓰고 싶은데, setAdapter 부분이나 getApplicationContext부분에서 에러가 떠서 해결을 못하고 있네요...
그냥 일반 클래스로 빼서 쓰면 되는거 아닌가요??? 아니면 다른 방법이 있을까요?