Java tutorial
/* * Copyright 2015 Rukmal Dias * * 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. */ package com.pg.newsapp.http; import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * NewsHttpClient creates HttpClient objects for different HTTP request types and returns a * String response if necessary. * * @author Rukmal Dias */ public class NewsHttpClient { public static final int REQ_TYPE_GET = 0; public static final int REQ_TYPE_POST = 1; public static final int REQ_TYPE_POST_COOKIES = 2; private static final String TAG = NewsHttpClient.class.getName(); private int mType; private String mUrl; private Map<String, String> mValues; private Map<String, String> mHeaders; private NewsHttpClient(int type, String url, Map<String, String> values, Map<String, String> headers) { mType = type; mUrl = url; mValues = values; mHeaders = headers; } /** * Executes a created http client object and returns a String response. * @return */ public synchronized String execute() { String result = null; if (mType == REQ_TYPE_POST) { result = executePost(); } else { result = executeGet(); } return result; } private String executePost() { String response = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(mUrl); // Add data if (mValues != null && mValues.size() > 0) { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(mValues.size()); for (String key : mValues.keySet()) { String value = mValues.get(key); nameValuePairs.add(new BasicNameValuePair(key, value)); } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } // Headers if (mHeaders != null && mHeaders.size() > 0) { for (String key : mHeaders.keySet()) { String value = mHeaders.get(key); httppost.addHeader(key, value); } } // ExecuteRequest HttpResponse httpResponse = httpclient.execute(httppost); response = new BasicResponseHandler().handleResponse(httpResponse); } catch (Exception e) { Log.w(TAG, "-- executePost " + e.getMessage()); } return response; } /** * TODO : ReCheck ... * @return */ private String executeGet() { String result = null; try { HttpClient httpclient = new DefaultHttpClient(); if (mValues != null && mValues.size() > 0) { StringBuilder sb = new StringBuilder(); for (String key : mValues.keySet()) { String value = mValues.get(key); if (sb.toString().length() > 0) { sb.append("&"); } sb.append(key); sb.append("="); sb.append(value); } mUrl += "?" + sb.toString(); } HttpGet httpget = new HttpGet(mUrl); ResponseHandler<String> responseHandler = new BasicResponseHandler(); result = httpclient.execute(httpget, responseHandler); } catch (Exception e) { Log.w(TAG, "-- executeGet" + e.getMessage()); } return result; } /** * Creates an http client instance * @param type * @param url * @param values * @return */ public static synchronized NewsHttpClient createInstance(int type, String url, Map<String, String> values) { NewsHttpClient instance = new NewsHttpClient(type, url, values, null); return instance; } /** * Creates an http client instance * @param type * @param url * @param values * @param headers * @return */ public static synchronized NewsHttpClient createInstance(int type, String url, Map<String, String> values, Map<String, String> headers) { NewsHttpClient instance = new NewsHttpClient(type, url, values, headers); return instance; } }