ウキャーってなったので作ってみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your_packagename_here; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.HttpStatus; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
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.client.methods.HttpUriRequest; | |
import org.apache.http.client.utils.URLEncodedUtils; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
public class EasyHttpClient { | |
protected String url; | |
protected HttpClient httpClient; | |
protected List<NameValuePair> params = new ArrayList<NameValuePair>(1); | |
public EasyHttpClient(String url) { | |
this.httpClient = new DefaultHttpClient(); | |
this.url = url; | |
} | |
public void addParam(String key, String value) { | |
this.params.add(new BasicNameValuePair(key, value)); | |
} | |
public String doGet() throws ClientProtocolException, IOException { | |
String queries = URLEncodedUtils.format(this.params, "UTF-8"); | |
HttpGet httpGet = new HttpGet(this.url + "?" + queries); | |
return this.doHttpRequest(httpGet); | |
} | |
public String doPost() throws UnsupportedEncodingException, ClientProtocolException, IOException { | |
UrlEncodedFormEntity entry = new UrlEncodedFormEntity(this.params); | |
HttpPost httpPost = new HttpPost(this.url); | |
httpPost.setEntity(entry); | |
return this.doHttpRequest(httpPost); | |
} | |
protected String doHttpRequest(HttpUriRequest request) throws ClientProtocolException, IOException { | |
String responseText = null; | |
HttpResponse response = this.httpClient.execute(request); | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
response.getEntity().writeTo(byteArrayOutputStream); | |
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | |
responseText = byteArrayOutputStream.toString(); | |
} | |
byteArrayOutputStream.close(); | |
return responseText; | |
} | |
} |
POST とGET しかありませんがいちおうこれで事足りるかなーと。
自分のパッケージ名に合わせていただければ幸いです。
使い方
EasyHttpClient easyHttpClient = EasyHttpClient("http://example.com"); // パラメータ追加 easyHttpClient.addParam("hoge", "1"); easyHttpClient.addParam("piyo", "2"); // 結果は文字列です。 String responseString; try { // post の場合 responseString = easyHttpClient.doPost(); // get の場合 responseString = easyHttpClient.doGet(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
よく JSON を受け取ります
try { // post の場合 responseString = easyHttpClient.doPost(); // get の場合 responseString = easyHttpClient.doGet(); // JSON を扱う JSONObject json = new JSONObject(responseString); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); }
非同期でもなんでもないのでレスポンス受け取るまで何もできませんが、
それはそれ、これはこれ・・・・。
改善・問題点どしどしください :)