- 테스트 성공 코드

- 개발환경 : Android Studio 3.2.1, API27(Android 8.1 Oreo), Eclipse Neon.3 Release (4.6.3), jdk1.8.0_92, Tomcat 9.0

 

AndroidManifest.xml 

<!-- 네트워크 사용에 대한 퍼미션 -->

<uses-permission android:name="android.permission.INTERNET" />

 

 

Android

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        HttpUtil data_transfer = new HttpUtil();

        JSONObject json_dataTransfer = new JSONObject();

        try

        {

            json_dataTransfer.put("HttpUtil", textView.getText());

            String json_string = json_dataTransfer.toString();

            String url = "http://[your domain]/[your web app]";

            data_transfer.execute(url, json_string);

        }

        catch (JSONException e)

        {

            e.printStackTrace();

        }

    }

 

    public class HttpUtil extends AsyncTask<String, Void, Void> {

 

        String res_json;

 

        @Override

        public Void doInBackground(String... params) {

            HttpURLConnection conn = null;

            try {

                conn = (HttpURLConnection) ((new URL(params[0]).openConnection()));

                conn.setReadTimeout(10000);

                conn.setConnectTimeout(15000);

                conn.setRequestMethod("POST");

                conn.setDoInput(true); // InputStream으로 서버로 부터 응답을 받겠다는 옵션.

                conn.setDoOutput(true); // OutputStream으로 POST 데이터를 넘겨주겠다는 옵션.

                conn.setRequestProperty("Accept-Charset", "UTF-8"); // Accept-Charset 설정.

                conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");

                conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

 

                //Write

                OutputStream os = conn.getOutputStream();

                os.write( params[1].getBytes("UTF-8") );

                os.flush();

                os.close();

 

                //Read

                BufferedReader br = null;

                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

                    br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

                } else {

                    br = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));

                }

 

                String line;

                StringBuffer response = new StringBuffer();

                while((line = br.readLine()) != null) {

                    response.append(line);

                    response.append('\r');

                }

                br.close();

                this.res_json = response.toString();

                //System.out.println(this.res_json);

            } catch (Exception e) {

                e.printStackTrace();

            }

            return null;

        }

 

        @Override

        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);

            MainActivity.serverRes.setText(this.res_json); //serverRes는 TextView로써 MainActivity Class에서 public static으로 선언되었고 onCreate에서 가져온다

        }

    }

 

 

Servlet

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));

        String json = "";

        if(br != null){

            json = br.readLine();

        }

        if (json.length() > 0)

            System.out.println("--"+json);

 

        JSONObject resJo = new JSONObject();

        resJo.put("RTN_MSG", "응답데이터입니다");

        this.printJSON(res, resJo.toJSONString());

    }

 

    private void printJSON(HttpServletResponse res, String jsonStr) {

        if (jsonStr != null) {

            res.setCharacterEncoding("UTF-8");

            res.setContentType("application/json");

            res.setHeader("Cache-Control", "no-cache");

            PrintWriter out;

            try {

                out = res.getWriter();

                out.print(jsonStr);

                out.flush();

                out.close();

            } catch (Exception e) {

                e.printStackTrace();

                System.out.println(e);

            }

        }

    }

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3132
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2370
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4025
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4035
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3850
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3826
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4105
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3985
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4106
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4021
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4180
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4224
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4083
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4017
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4113
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3964