AndroidでJSON形式のAPIのを叩くときに便利です。

GET,POST,DELETEのAPIを叩く必要があるときは適当に以下のクラスファイルを用意して、Network.メソッド名(URL名)でだいたいのJSONデータが取れると思います。

JSONのデータ操作については別の機会で。


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class Network {

	/**
	 * GET API
	 */
	public static JSONObject getDataGET_API(String url) {
		HttpClient objHttp = new DefaultHttpClient();
		String sReturn = "";
		JSONObject jsonData = null;
		try {
			HttpGet objGet = new HttpGet(url);
			HttpResponse objResponse = objHttp.execute(objGet);

			if (objResponse.getStatusLine().getStatusCode() < 400) {
				InputStream objStream = objResponse.getEntity().getContent();
				InputStreamReader objReader = new InputStreamReader(objStream);
				BufferedReader objBuf = new BufferedReader(objReader);
				StringBuilder objJson = new StringBuilder();
				String sLine;
				while ((sLine = objBuf.readLine()) != null) {
					objJson.append(sLine);
				}
				sReturn = objJson.toString();
				try {
					jsonData = new JSONObject(sReturn);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				Logger.e("jsonData:" + jsonData.toString());
				objStream.close();
				return jsonData;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ログアウト用 DELETE API
	 */
	public static JSONObject getDataDELETE_API(String url) {
		HttpClient objHttp = new DefaultHttpClient();
		String sReturn = "";
		JSONObject jsonData = null;
		try {
			HttpDelete objeDelete = new HttpDelete(url);
			HttpResponse objResponse = objHttp.execute(objeDelete);
			if (objResponse.getStatusLine().getStatusCode() < 400) {
				InputStream objStream = objResponse.getEntity().getContent();
				InputStreamReader objReader = new InputStreamReader(objStream);
				BufferedReader objBuf = new BufferedReader(objReader);
				StringBuilder objJson = new StringBuilder();
				String sLine;
				while ((sLine = objBuf.readLine()) != null) {
					objJson.append(sLine);
				}
				sReturn = objJson.toString();
				try {
					jsonData = new JSONObject(sReturn);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				objStream.close();
				return jsonData;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ログイン用 POST API List<NameValuePair> objValuePairs = new
	 * ArrayList<NameValuePair>(); objValuePairs.add(new
	 * BasicNameValuePair("account", Common.ACCOUNT)); objValuePairs.add(new
	 * BasicNameValuePair("password", Common.SHA));
	 */
	public static JSONObject getDataPOST_API(String url,
			List<NameValuePair> objValuePairs) {
		HttpClient objHttp = new DefaultHttpClient();
		String sReturn = "";
		JSONObject jsonData = null;
		try {

			HttpPost objPost = new HttpPost(url);
			objPost.setEntity(new UrlEncodedFormEntity(objValuePairs, "UTF-8"));

			HttpResponse objResponse = objHttp.execute(objPost);
			if (objResponse.getStatusLine().getStatusCode() < 400) {
				InputStream objStream = objResponse.getEntity().getContent();
				InputStreamReader objReader = new InputStreamReader(objStream);
				BufferedReader objBuf = new BufferedReader(objReader);
				StringBuilder objJson = new StringBuilder();
				String sLine;
				while ((sLine = objBuf.readLine()) != null) {
					objJson.append(sLine);
				}
				sReturn = objJson.toString();
				try {
					jsonData = new JSONObject(sReturn);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				objStream.close();
				return jsonData;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}
}

他の人とやってるとことは大差ないですが、結構使いやすいんじゃないかと思います。
JSONObjectの処理はまた別の機会で。

・GET
 引数にURLを入れると、JSONObjectが返ってきます。単純なデータ取得で使うかも。

  JSONObject json = Network.getDataGET_API("http://...");

・POST
 POSTにはパラメータの準備が必要です。ログインAPIで使うかも。

  //パラメータ設定
  List<NameValuePair> objValuePairs = new ArrayList<NameValuePair>(); 
  objValuePairs.add(new BasicNameValuePair("account", "hogehoge"));
  objValuePairs.add(new BasicNameValuePair("password", "hugahuga"));
  
  //データ取得
  JSONObject json = Network.getDataPOST_API("http://...",objValuePairs);

・DELETE
 やることはGETと同じ。ログアウトのAPIとかで使うかも。

  JSONObject json = Network.getDataDELETE_API("http://...");

※JsonデータがNullだったらネットワークの接続に失敗してるかもしれません。
※JSONデータで失敗のデータを取得した場合、指定のパラメータ等が適切ではなさそうです。