관리 메뉴

moozi

json데이터 listview에 출력하기 본문

안드로이드개발강좌

json데이터 listview에 출력하기

moozi 2017. 9. 29. 11:33

package com.naver.jspconn02;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView lv;
    ArrayList<String> nameArr;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView)findViewById(R.id.listView);
        nameArr=new ArrayList<String>();

        //JSP연동.json파싱해서 ArrayList에 추가
        String url="http://10.0.2.2:8181/AndroidConn01/CustomersDB.jsp";
        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,nameArr);
        lv.setAdapter(adapter);

        //AsyncTask실행
        new DownloadWebpageTask().execute(url);
    }


    private class DownloadWebpageTask extends AsyncTask<String,Void,String> {

        //주요 내용 실행
        @Override
        protected String doInBackground(String... urls) {
            try {
                return (String)downloadUrl((String)urls[0]);
            } catch (IOException e) {
                return "다운로드 실패";
            }
        }

        private String downloadUrl(String myurl) throws IOException {

            HttpURLConnection conn = null;
            try {
                URL url = new URL(myurl);
                conn = (HttpURLConnection) url.openConnection();
                BufferedInputStream buf = new BufferedInputStream(conn.getInputStream());
                BufferedReader bufreader = new BufferedReader(new InputStreamReader(buf, "utf-8"));
                String line = null;
                String page = "";
                while((line = bufreader.readLine()) != null) {
                    page += line;
                }

                return page;
            } finally {
                conn.disconnect();
            }
        }

        //ui변경 작업 실행
        @Override
        protected void onPostExecute(String result) {

            try {

                JSONObject json = new JSONObject(result);
                JSONArray jArr = json.getJSONArray("customers");

                for (int i=0; i<jArr.length(); i++) {

                    json = jArr.getJSONObject(i);

                    String name    = json.getString("name");

                    //name을 ArrayList에 추가
                    nameArr.add(name);
                    adapter.notifyDataSetChanged();//변경내용 반영
                }
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    }
}


 

'안드로이드개발강좌' 카테고리의 다른 글

CustomerSearch.jsp  (0) 2017.09.29
json데이터 java코드로 뷰에 출력하기  (0) 2017.09.29
customerdb.jsp  (0) 2017.09.29
jspconn01  (0) 2017.09.29
customers.jsp  (0) 2017.09.29
Comments