2019. 1. 30. 21:09ㆍ[정리] 직무별 개념 정리/안드로이드
import android.content.ContentValues; import android.os.AsyncTask; import android.util.Log; import org.json.JSONObject; public class RestAPIAsyncTask extends AsyncTask<Void, Void, JSONObject>{ private String url; private ContentValues values; public RestAPIAsyncTask(String url, ContentValues values){ this.url = url; this.values = values; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected JSONObject doInBackground(Void... params) { RestAPIConnection restAPIConnection = new RestAPIConnection(); return restAPIConnection.requestPost(url, values); } @Override protected void onProgressUpdate(Void... progress) { } @Override protected void onPostExecute(JSONObject result) { Log.v("test", result.toString()); } }
|
RestAPIConnection
import android.content.ContentValues; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; public class RestAPIConnection { private final int READ_TIMEOUT = 10; private final int CONNECT_TIMEOUT = 10; // ContentValues to JSON private JSONObject ContentValuesToStringBuffer(ContentValues params) throws JSONException { JSONObject jsonObject = new JSONObject(); if (params == null) return jsonObject; for (Map.Entry<String, Object> parameter : params.valueSet()) jsonObject.put(parameter.getKey(), parameter.getValue().toString()); return jsonObject; } public JSONObject requestPost(String requestURL, ContentValues params) { HttpURLConnection urlConnection = null; OutputStream outputStream = null; InputStream inputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { URL url = new URL(requestURL); JSONObject jsonObject = ContentValuesToStringBuffer(params); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(READ_TIMEOUT * 1000); urlConnection.setConnectTimeout(CONNECT_TIMEOUT * 1000); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setRequestProperty("Accept-Charset", "utf-8"); // Accept-Charset 설정. urlConnection.setRequestProperty("Cache-Control", "no-cache"); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestProperty("Accept", "application/json"); // Output stream으로 json을 보냄 outputStream = urlConnection.getOutputStream(); outputStream.write(jsonObject.toString().getBytes()); outputStream.flush(); // 실패할 경우 null return int responseCode = urlConnection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) return null; // 성공할 경우 inputStream = urlConnection.getInputStream(); byteArrayOutputStream = new ByteArrayOutputStream(); byte[] byteBuffer = new byte[1024]; byte[] byteData = null; int nLength = 0; while((nLength = inputStream.read(byteBuffer, 0, byteBuffer.length)) != -1) { byteArrayOutputStream.write(byteBuffer, 0, nLength); } byteData = byteArrayOutputStream.toByteArray(); String response = new String(byteData); JSONObject responseJSON = new JSONObject(response); return responseJSON; } catch(JSONException e){ e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 모든 connection, stream을 닫음 if (urlConnection != null) urlConnection.disconnect(); if (outputStream != null) try{outputStream.close();} catch(Exception e){e.printStackTrace();}; if (inputStream != null) try{inputStream.close();} catch(Exception e){e.printStackTrace();}; if (byteArrayOutputStream != null) try{byteArrayOutputStream.close();} catch(Exception e){e.printStackTrace();}; } return null; } } |
'[정리] 직무별 개념 정리 > 안드로이드' 카테고리의 다른 글
[2019.02.11] Retrofit custom builder (0) | 2019.02.11 |
---|---|
[2019.02.09] Retrofit 요약 (0) | 2019.02.09 |