소스 참고 사이트 :  http://micropilot.tistory.com/1657

위의 사이트에서 소스를 참고해서 테스트 중입니다.

아래의 소스를 사용하면 서버에 접속해서 값을 잘 받아옵니다.

 

문제는...

헤더 설정이 필요해서 아래의 빨간 부분을 추가하면... 서버에 접속을 못합니다.

왜 그런건가요?

 

고수님들 한번 봐주세요~!

try {
            // Construct data
            String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
            data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("홍길동", "UTF-8");
       
            // Send data
            URL url = new URL("http://192.168.123.100:80/HelloWeb/androidPost.jsp");
            URLConnection conn = url.openConnection();

 

            //이걸 추가하면 서버에서 접속을 못함

            conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + strBoundary);


            // If you invoke the method setDoOutput(true) on the URLConnection, it will always use the POST method.
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
       
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line = null;
            String response = "";
            while ((line = rd.readLine()) != null) {
               response += line;
            }
            textview.setText(response);
            wr.close();
            rd.close();
        }
        catch (Exception e) {
        }