관리 메뉴

moozi

json데이터 java코드로 뷰에 출력하기 본문

안드로이드개발강좌

json데이터 java코드로 뷰에 출력하기

moozi 2017. 9. 29. 11:49

package com.naver.jspconn03;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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 {

    LinearLayout linearLayout1;

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

        //LinearLayout찾기
        linearLayout1=(LinearLayout)findViewById(R.id.linearLayout);

        //JSP연동.json파싱해서 ArrayList에 추가
        String url="http://10.0.2.2:8181/AndroidConn01/CustomersDB.jsp";

        //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);

                    final String id    = json.getString("id");
                    String name    = json.getString("name");
                    String address = json.getString("address");

                    LinearLayout linear = new LinearLayout(getApplicationContext());
                    linear.setOrientation(LinearLayout.HORIZONTAL);

                    TextView textView = new TextView(getApplicationContext());
                    textView.setWidth(150);
                    textView.setText(id);
                    textView.setId(i);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getApplicationContext(),id,Toast.LENGTH_LONG).show();
                        }
                    });
                    linear.addView(textView);


                    TextView textView1 = new TextView(getApplicationContext());
                    textView1.setWidth(150);
                    textView1.setText(name);
                    linear.addView(textView1);

                    TextView textView2 = new TextView(getApplicationContext());
                    textView2.setWidth(500);
                    textView2.setText(address);
                    linear.addView(textView2);

                    linearLayout1.addView(linear);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

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

android에서 get,post로 데이터 넘기기  (0) 2017.09.29
CustomerSearch.jsp  (0) 2017.09.29
json데이터 java코드로 뷰에 출력하기  (0) 2017.09.29
json데이터 listview에 출력하기  (0) 2017.09.29
customerdb.jsp  (0) 2017.09.29
jspconn01  (0) 2017.09.29
0 Comments
댓글쓰기 폼