하고재비

[Android] JSON을 GSON으로 파싱하기 본문

Android

[Android] JSON을 GSON으로 파싱하기

DeadDE 2018. 2. 14. 15:12

MainActivity

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
public class MainActivity extends AppCompatActivity {
 
    String ss;  // 전송값 저장
    UserInfo userInfo;
    Gson gson = new GsonBuilder().create();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        try {
            ss = new Select().execute("").get();
            Log.e("main_json.get()",ss);
            userInfo = gson.fromJson(ss,UserInfo.class);
            Log.e("gson main",userInfo.getID());
            TextView t1 = (TextView)findViewById(R.id.tv_id);
            t1.setText(userInfo.getID());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
 
 
 
    }
}
cs


Select


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
public class Select extends AsyncTask<String, Void, String> {
    private URL url;
    private String URL_Address = "JSON URL";
    String res;
 
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
 
    @Override
    protected String doInBackground(String... strings) {
        res = "";
        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");
            }
            res = builder.toString();
 
        }  catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("Get result",res);
        return res;
    }
 
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
 
    }
}
 
cs

UserInfo
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
package com.example.user.jsonpaser;
 
import android.util.Log;
 
/**
 * Created by USER on 2018-02-14.
 */
 
public class UserInfo {
    public String getID() {
        return ID;
    }
 
    public void setID(String ID) {
        this.ID = ID;
    }
 
    public String getNAME() {
        return NAME;
    }
 
    public void setNAME(String NAME) {
        this.NAME = NAME;
    }
 
    public String getPHONE() {
        return PHONE;
    }
 
    public void setPHONE(String PHONE) {
        this.PHONE = PHONE;
    }
 
    public String getGRADE() {
        return GRADE;
    }
 
    public void setGRADE(String GRADE) {
        this.GRADE = GRADE;
    }
 
    public String getWRITE_TIME() {
        return WRITE_TIME;
    }
 
    public void setWRITE_TIME(String WRITE_TIME) {
        this.WRITE_TIME = WRITE_TIME;
    }
 
    String ID;
    String NAME;
    String PHONE;
    String GRADE;
    String WRITE_TIME;
 
}
cs

주의 : Userinfo의 변수명들이 JSON파일의 key값과 동일해야함.


Comments