Back to project page android-mvc-framework.
The source code is released under:
Apache License
If you think the Android project android-mvc-framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.android_mvc.framework.net; /*www. ja va 2 s . co m*/ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; 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.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.android_mvc.framework.common.FWUtil; /** * ????????HTTP??????????? * @author id:language_and_engineering * */ public class HttpPostRequest { // NOTE: ??????????? // http://d.hatena.ne.jp/language_and_engineering/20111121/p1 // NOTE: ?BL??????????????????????????????????? // ??????????????????????????????????????????????????????????????????? // ????????????????????????????????????????????? // TODO: https??????GET????? // ???? private String request_encoding = "UTF-8"; private String response_encoding = "UTF-8"; // ??? private String target_url; private List<NameValuePair> post_params = new ArrayList<NameValuePair>(); // ?? HttpPostResponse res = new HttpPostResponse(); /** * ?????????URL???? */ public HttpPostRequest target(String url) { this.target_url = url; FWUtil.d("this.target_url???????" + this.target_url); return this; } /** * POST???????? */ public HttpPostRequest addParam(String post_name, String post_value) { post_params.add(new BasicNameValuePair( post_name, post_value )); FWUtil.d( "POST?????????" + post_name + "?????" + post_value + "???????" ); return this; } /** * ???????????????????????? */ public HttpPostResponse getResponse() { if(target_url == null) { FWUtil.e("URL????????????????????????"); return null; } FWUtil.d("post???????????"); // URL URI urlObj; try { urlObj = new URI( target_url ); FWUtil.d("URL???OK"); } catch (URISyntaxException e) { e.printStackTrace(); return res.err( "??????URL" ); } // POST????????????POST???????? HttpPost request = new HttpPost( urlObj ); try { // ??????????????????? request.setEntity(new UrlEncodedFormEntity(post_params, request_encoding)); FWUtil.d( "????????OK" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return res.err( "???????????" ); } // POST???????? DefaultHttpClient httpClient = new DefaultHttpClient(); FWUtil.d("POST??"); try { // ?????????????????handler???HttpResponse??????????? httpClient.execute( request, response_handler ); } catch (ClientProtocolException e) { e.printStackTrace(); return res.err( "???????????" ); } catch (IOException e) { e.printStackTrace(); return res.err( "IO???" ); } // shutdown????????????????????????? httpClient.getConnectionManager().shutdown(); return res; } /** * ??????????????????????????????????????? */ private ResponseHandler<Void> response_handler = new ResponseHandler<Void>() { // HTTP?????????????????????????????????????????????? @Override public Void handleResponse(HttpResponse response) throws IOException { int response_code = response.getStatusLine().getStatusCode(); FWUtil.d( "??????????" + response_code ); // ???????????????? switch ( response_code ) { // ?????????????????????????200 case HttpStatus.SC_OK: FWUtil.d("????????????????"); // ??????????????????????????????????????????? // ?IOException???????????? String response_text = EntityUtils.toString( response.getEntity(), HttpPostRequest.this.response_encoding ); res.setTextOnSuccess( response_text ); break; // 404 case HttpStatus.SC_NOT_FOUND: FWUtil.d("???????????"); res.err( "404 Not Found" ); break; default: FWUtil.d("?????"); res.err( "??????????" ); } return null; } }; }