Java tutorial
package kr.moonlightdriver.app.server; import android.os.AsyncTask; import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NoHttpResponseException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.UnsupportedEncodingException; /** * Restor - REST API ? REST Client ??. * * Copyright 2013 Hyojun Kim. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Restor { /** * HTTP Post ?? ??. */ private static class PostTask extends AsyncTask<HttpEntity, Long, String> { private Callback mListener; private String URL, cancelReason; private Result mResult; public PostTask(String URL, Callback listener) { mListener = listener; this.URL = URL; } @Override protected String doInBackground(HttpEntity... entities) { Log.e("Restor", "Doin in background"); DefaultHttpClient httpClient = new DefaultHttpClient(); String result = ""; try { HttpPost post = new HttpPost(URL); // URL . HttpEntity entity = entities[0]; post.setHeader("Content-Type", "application/json"); // ? ? MultipartEntity? // ? publishProgress // onProgressUpdate . if (entity instanceof MultipartPostable && mListener != null) { ((MultipartPostable) entity).setOnCountingListener(new MultipartPostable.OnCountingListener() { @Override public void onCount(long transferred) { publishProgress(transferred); } }); } post.setEntity(entity); // // POST! HttpResponse response = httpClient.execute(post); // ? ? HttpEntity resEntity = response.getEntity(); if (resEntity == null) throw new NoHttpResponseException("resEntity == null"); result = EntityUtils.toString(resEntity); } catch (Exception e) { cancelReason = e.getClass().getSimpleName() + " - " + e.getMessage(); Log.e("Restor", "PostTask cancelled!: " + cancelReason); // : > cancel(true); } httpClient.getConnectionManager().shutdown(); return result; } @Override protected void onCancelled() { super.onCancelled(); if (mListener != null) { mListener.onCancelled(cancelReason); } } @Override protected void onPostExecute(String result) { super.onPostExecute(result); mResult = new Result(result); if (mListener != null) { mListener.onComplete(mResult); } } @Override protected void onProgressUpdate(Long... progress) { super.onProgressUpdate(progress); if (mListener != null && mListener instanceof MultipartCallback) { ((MultipartCallback) mListener).onProgress(progress[0]); } } /** * ? . */ public Result getResult() { return mResult; } } /** * ? REST API URL? GET . * ?: ???. ? onComplete ? . * * @param url REST API? URL * @param callback / ? */ /* public static void Call(String url, Callback callback) { new PostTask(url, callback).execute(); }*/ /** * Form? Entity? URL POST. * ?: ???. ? onComplete ? . * * @param url REST API? URL * @param form POST ? * @param callback / ? */ public static void Call(String url, Form form, Callback callback) { new PostTask(url, callback).execute(form.toEntity()); } /** * Form? Entity? URL POST. * ?: ???. ? onComplete ? . * * @param url REST API? URL * @param json POST ? * @param callback / ? */ public static void Call(String url, String json, Callback callback) { try { new PostTask(url, callback).execute(new ByteArrayEntity(json.getBytes("UTF8"))); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * MultipartEntity? URL POST. * ?: ???. ? onComplete ? . * Callback MultipartCallback? ? . * * @param url REST API? URL * @param entity ? * @param callback / ? (MutlpartCallback ) */ public static void Call(String url, MultipartEntity entity, Callback callback) { new PostTask(url, callback).execute(entity); } /** * ? REST API URL? GET . * ?: ??(Sync)?. ?? ? ANR? ?. * * @param url REST API? URL */ /* public static Result Call(String url) { PostTask task = new PostTask(url, null); try { task.execute().wait(); } catch (InterruptedException e) { e.printStackTrace(); } return task.getResult(); }*/ /** * Form? Entity? URL POST. * ?: ??(Sync)?. ?? ? ANR? ?. * * @param url REST API? URL * @param form POST ? */ /* public static Result Call(String url, Form form) { PostTask task = new PostTask(url, null); try { task.execute(form.toEntity()).wait(); } catch (InterruptedException e) { e.printStackTrace(); } return task.getResult(); }*/ /** * MultipartEntity? URL POST. * ?: ??(Sync)?. ?? ? ANR? ?. * * @param url REST API? URL * @param entity ? *h */ /* public static Result Call(String url, MultipartEntity entity) { PostTask task = new PostTask(url, null); try { task.execute(entity).wait(); } catch (InterruptedException e) { e.printStackTrace(); } return task.getResult(); }*/ }