HttpURLConnection이 Httppost와 다른 이유는 전송 데이터를 URL에 넣어 전송하느냐 body에 넣어 전송하느냐이다.
후자가 데이터를 감추기에 더 좋을꺼 같긴하다..
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 | try { URL obj = new URL(url+"?"+paramString.toString()); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.setDefaultUseCaches(false); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream())); pw.write(paramString); pw.flush(); pw.close(); int retCode = conn.getResponseCode(); if (retCode == HttpURLConnection.HTTP_OK) { InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)."UTF-8"); String line; StringBuffer response = new StringBuffer(); while((line = br.readLine()) != null){ response.append(line); } br.close(); Log.d(TAG,"responvalue: "+response.toString()); return response.toString(); }else{ Log.d(TAG,"error code is " + retCode); } } catch (Exception e) { e.printStackTrace(); } | cs |
22번 라인에 "UTF-8"을 추가하면 한글깨짐현상을 막을수 있다.
'Programming > Android' 카테고리의 다른 글
어플 패키지명 (0) | 2016.08.25 |
---|---|
ProgressDialog 와 Toast (0) | 2016.08.25 |
SharedPreferences (0) | 2016.08.24 |
Context (0) | 2016.08.24 |
Activity 생명주기 (0) | 2016.08.24 |