하고재비

[Android] Json 파싱하기 본문

Android

[Android] Json 파싱하기

DeadDE 2018. 2. 14. 11:10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class JsonPaser  extends AsyncTask<String, Void, String> {
    private URL url;
    private String URL_Address = "~~~/TestDB.jsp";
 
    public String getRes() {
        return res;
    }
 
    public void setRes(String res) {
        this.res = res;
    }
 
    private String res;
 
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
 
 
    @Override
    protected String doInBackground(String... strings) {
        this.setRes("");
        try {
            url = new URL(URL_Address);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 
            conn.setDefaultUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST"); // content type 설정
            conn.setRequestProperty("Content-type","application/x-www-form-urlencoded; utf-8");
 
            //전송값 설정
            StringBuffer buffer = new StringBuffer();
            buffer.append("id").append("=").append(strings[0]);
 
            //서버로 전송
            OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
            PrintWriter writer = new PrintWriter(outStream);
            writer.write(buffer.toString());
            writer.flush();
 
            StringBuilder builder = new StringBuilder();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null){
                builder.append(line+"\n");
            }
            this.setRes(builder.toString());
 
        }  catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("Get result",getRes());
        return getRes();
    }
 
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
 
    }
}
 
cs


MainActivity.class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity {
    JsonPaser jsonPaser = new JsonPaser();
    String ss;  // 전송값 저장
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            ss = jsonPaser.execute("").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        TextView t = (TextView)findViewById(R.id.text);
        t.setText(ss);
    }
}
cs

~~~/testDB.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
    "datas": [{
        "ID": "avoehddjs",
        "NAME": "dongeon",
        "PHONE": "1111",
        "GRADE": "1",
        "WRITE_TIME": "18/01/11"
    }, {
        "ID": "1",
        "NAME": "1",
        "PHONE": "1",
        "GRADE": "1",
        "WRITE_TIME": "18/02/13"
    }, {
        "ID": "iu",
        "NAME": "이지은",
        "PHONE": "2222",
        "GRADE": "z",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "TOP",
        "NAME": "최승현",
        "PHONE": "1111",
        "GRADE": "z",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "brad",
        "NAME": "브래드핏",
        "PHONE": "4444",
        "GRADE": "10",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "yasdasdasdr",
        "NAME": "aslkdjalskdj",
        "PHONE": "5555",
        "GRADE": "5",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "suji",
        "NAME": "배수지",
        "PHONE": "3333",
        "GRADE": "3",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "gd",
        "NAME": "권지용",
        "PHONE": "6666",
        "GRADE": "6",
        "WRITE_TIME": "17/12/21"
    }, {
        "ID": "a",
        "NAME": "a",
        "PHONE": "a",
        "GRADE": "a",
        "WRITE_TIME": "18/02/09"
    }, {
        "ID": "dd",
        "NAME": "dd",
        "PHONE": "dd",
        "GRADE": "dd",
        "WRITE_TIME": "18/02/09"
    }]
}
cs

결과