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.old; //from w w w . ja v a2s.c o 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; import com.android_mvc.framework.task.SequentialAsyncTask; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; /** * HTTP?????POST?????????????????????????????????? * @author id:language_and_engineering * */ public class HttpPostTask extends SequentialAsyncTask { // NOTE: ??????????????????????????????? // @see http://d.hatena.ne.jp/language_and_engineering/20111121/p1 // ???? private String request_encoding = "UTF-8"; private String response_encoding = "UTF-8"; // ?????? protected Activity parent_activity = null; protected String post_url = null; protected Handler result_handler = null; protected List<NameValuePair> post_params = null; // ???????????????? private ResponseHandler<Void> response_handler = null; private String http_err_msg = null; private String http_ret_msg = null; private ProgressDialog dialog = null; private boolean isDialogRequiredFlag = false; private boolean dump_response_when_success = true; // ?????????????????????????????????????? /** * ???? */ public HttpPostTask( Activity parent_activity, String post_url, Handler result_handler ) { // ???? if( parent_activity != null) { this.parent_activity = parent_activity; setDialogRequired( true ); } this.post_url = post_url; this.result_handler = result_handler; // ??????????????????????new????set??????? post_params = new ArrayList<NameValuePair>(); } /** * ???????????????????????????????????????????????????????????? */ public HttpPostTask() { // ???????OK } /** * ???????????????????????????????? */ protected boolean isDialogRequired() { return isDialogRequiredFlag; } /** * ???????????????? */ public void setDialogRequired( boolean b ) { FWUtil.d("POST??????????????????" + b); this.isDialogRequiredFlag = b; } /* --------------------- POST????? --------------------- */ /** * ?? */ public void addPostParam( String post_name, String post_value ) { post_params.add(new BasicNameValuePair( post_name, post_value )); FWUtil.d( "POST?????????" + post_name + "?????" + post_value + "???????" ); } /* --------------------- ?????? --------------------- */ /** * ?????? */ @Override protected void onPreExecuteHook() { // ???????? if( isDialogRequired() ) { FWUtil.d("???????????????"); dialog = new ProgressDialog( parent_activity ); dialog.setMessage("??????"); dialog.show(); } else { FWUtil.d("????????????????????????????????????"); } // ??????????????? response_handler = new ResponseHandler<Void>() { // HTTP?????????????????????????????????????????????? @Override public Void handleResponse(HttpResponse response) throws IOException { FWUtil.d( "??????????" + response.getStatusLine().getStatusCode() ); // ?????????????????????????200 switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: FWUtil.d("????????????????"); // ??????????????????????????????????????????? // ?IOException???????????? HttpPostTask.this.http_ret_msg = EntityUtils.toString( response.getEntity(), HttpPostTask.this.response_encoding ); break; case HttpStatus.SC_NOT_FOUND: // 404 FWUtil.d("???????????"); HttpPostTask.this.http_err_msg = "404 Not Found"; break; default: FWUtil.d("?????"); HttpPostTask.this.http_err_msg = "??????????"; } return null; } }; } /** * ??????? */ @Override protected boolean main() { FWUtil.d("post?????????"); // URL URI url = null; try { url = new URI( post_url ); FWUtil.d("URL???OK"); } catch (URISyntaxException e) { e.printStackTrace(); http_err_msg = "??????URL"; } // POST????????????POST???????? HttpPost request = new HttpPost( url ); try { // ??????????????????? request.setEntity(new UrlEncodedFormEntity(post_params, request_encoding)); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); http_err_msg = "???????????"; } // POST???????? DefaultHttpClient httpClient = new DefaultHttpClient(); FWUtil.d("POST??"); try { httpClient.execute(request, response_handler); } catch (ClientProtocolException e) { e.printStackTrace(); http_err_msg = "???????????"; } catch (IOException e) { e.printStackTrace(); http_err_msg = "IO???"; } // shutdown????????????????????????? httpClient.getConnectionManager().shutdown(); // ------- ???????TODO:RF -------- // ?????????? if( isDialogRequired() ) { dialog.dismiss(); } // ????????UI???????????????????????? Message message = new Message(); Bundle bundle = new Bundle(); if (http_err_msg != null) { // ?????? bundle.putBoolean("http_post_success", false); bundle.putString("http_response", http_err_msg); FWUtil.d("??????????????" + http_err_msg); } else { // ?????? bundle.putBoolean("http_post_success", true); bundle.putString("http_response", http_ret_msg); FWUtil.d("????????????" ); if( dump_response_when_success ) { FWUtil.d( "???????" + http_ret_msg ); } } message.setData(bundle); // ????????????????????UI?????????? result_handler.sendMessage(message); // ????????????????? if (http_err_msg != null) { return false; } else { return true; } } // G&S public boolean isDump_response_when_success() { return dump_response_when_success; } public void setDump_response_when_success(boolean dumpResponseWhenSuccess) { dump_response_when_success = dumpResponseWhenSuccess; } }