Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dv.sharer.mobile.rest.impl; import android.util.Log; import com.dv.common.webservices.rest.core.client.IRestClient; import com.dv.common.webservices.rest.core.client.IRestClientModel; import com.dv.common.webservices.rest.core.client.IUploadDownload; import com.dv.common.webservices.rest.core.response.Response; import com.dv.model.pojo.Image; import com.dv.sharer.mobile.rest.AndroidRestClient; import java.io.IOException; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; /** * * @author PP76575 */ public class ImageAndroidRestClient extends AndroidRestClient implements IUploadDownload, IRestClient { private static final String TAG = ImageAndroidRestClient.class.getName(); public ImageAndroidRestClient(IRestClientModel<Image> model, boolean logMessages) { super(model, logMessages); } public byte[] downloadFile(String id) throws Exception { String url = getUrlAndPath() + id; String response; try { HttpGet get = getDownloadGET(url); HttpResponse httpResponse = getHttpclient().execute(get); response = getResponse(httpResponse); } catch (Exception e) { Log.e(TAG, "Error handling update", e); throw e; } return Base64.decodeBase64(response); } public byte[] downloadFile(Integer intgr) throws Exception { return downloadFile(intgr.toString()); } public Response uploadFile(Integer intgr, byte[] data) throws Exception { data = Base64.encodeBase64(data); String dataStr = new String(data); String url = getUrlAndPath() + "upload/"; String response; try { HttpPost post = getUploadPOST(url, dataStr); HttpResponse httpResponse = getHttpclient().execute(post); response = getResponse(httpResponse); } catch (Exception e) { Log.e(TAG, "Error handling update", e); throw e; } return Response.fromJson(response, Response.class); } protected HttpPost getUploadPOST(String urlStr, String data) throws IOException { HttpPost httppost = new HttpPost(urlStr); httppost.setHeader("Content-Type", "text/plain"); StringEntity entity = new StringEntity(data); entity.setContentType("application/json"); httppost.addHeader("Accept", "application/json"); httppost.setEntity(entity); return httppost; } protected HttpGet getDownloadGET(String urlStr) throws IOException { HttpGet httpGet = new HttpGet(urlStr); httpGet.setHeader("Content-Type", "application/json"); httpGet.addHeader("Accept", "text/plain"); return httpGet; } }