Android Open Source - android-mvc-framework Http Post Request






From Project

Back to project page android-mvc-framework.

License

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.

Java Source Code

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;
        }

    };

}




Java Source Code List

com.android_mvc.framework.activities.CommonActivityUtil.java
com.android_mvc.framework.activities.IBaseActivity.java
com.android_mvc.framework.activities.base.BaseMapActivity.java
com.android_mvc.framework.activities.base.BaseNormalActivity.java
com.android_mvc.framework.activities.base.BaseTabHostActivity.java
com.android_mvc.framework.activities.installation.InstallAppFWBaseActivity.java
com.android_mvc.framework.annotations.SuppressDebugLog.java
com.android_mvc.framework.common.AbstractAppSettings.java
com.android_mvc.framework.common.BaseUtil.java
com.android_mvc.framework.common.FWUtil.java
com.android_mvc.framework.controller.BaseController.java
com.android_mvc.framework.controller.ControlFlowDetail.java
com.android_mvc.framework.controller.action.ActionResult.java
com.android_mvc.framework.controller.action.BLExecutor.java
com.android_mvc.framework.controller.action.BaseAction.java
com.android_mvc.framework.controller.routing.IntentPortable.java
com.android_mvc.framework.controller.routing.Router.java
com.android_mvc.framework.controller.routing.RoutingTable.java
com.android_mvc.framework.controller.routing.TabContentMapping.java
com.android_mvc.framework.controller.validation.ActivityParams.java
com.android_mvc.framework.controller.validation.BaseValidationsUtil.java
com.android_mvc.framework.controller.validation.NumberComparator.java
com.android_mvc.framework.controller.validation.ValidationExecutor.java
com.android_mvc.framework.controller.validation.ValidationResult.java
com.android_mvc.framework.db.DBHelper.java
com.android_mvc.framework.db.dao.BaseDAO.java
com.android_mvc.framework.db.dao.BaseDBDAO.java
com.android_mvc.framework.db.dao.BasePrefDAO.java
com.android_mvc.framework.db.dao.FWPrefDAO.java
com.android_mvc.framework.db.dao.Finder.java
com.android_mvc.framework.db.dao.IFWDAO.java
com.android_mvc.framework.db.entity.BaseLogicalEntity.java
com.android_mvc.framework.db.schema.AbstractSchemaDefinition.java
com.android_mvc.framework.db.schema.RDBColumn.java
com.android_mvc.framework.db.schema.RDBSchema.java
com.android_mvc.framework.db.schema.RDBTable.java
com.android_mvc.framework.db.transaction.todo.TxnListener.java
com.android_mvc.framework.db.transaction.todo.TxnScope.java
com.android_mvc.framework.net.HttpPostRequest.java
com.android_mvc.framework.net.HttpPostResponse.java
com.android_mvc.framework.net.old.HttpPostHandler.java
com.android_mvc.framework.net.old.HttpPostTask.java
com.android_mvc.framework.task.AsyncTasksRunner.java
com.android_mvc.framework.task.RunnerFollower.java
com.android_mvc.framework.task.SequentialAsyncTask.java
com.android_mvc.framework.ui.UIBuilder.java
com.android_mvc.framework.ui.UIUtil.java
com.android_mvc.framework.ui.menu.OptionMenuBuilder.java
com.android_mvc.framework.ui.menu.OptionMenuDescription.java
com.android_mvc.framework.ui.tab.TabDescription.java
com.android_mvc.framework.ui.tab.TabHostBuilder.java
com.android_mvc.framework.ui.view.IFWView.java
com.android_mvc.framework.ui.view.MButton.java
com.android_mvc.framework.ui.view.MCheckBox.java
com.android_mvc.framework.ui.view.MEditText.java
com.android_mvc.framework.ui.view.MLinearLayout.java
com.android_mvc.framework.ui.view.MTextView.java
com.android_mvc.sample_project.activities.func_db.DBEditActivity.java
com.android_mvc.sample_project.activities.func_db.DBListActivity.java
com.android_mvc.sample_project.activities.func_db.SampleTabHostActivity.java
com.android_mvc.sample_project.activities.func_net.HttpNetActivity.java
com.android_mvc.sample_project.activities.installation.InstallAppActivity.java
com.android_mvc.sample_project.activities.installation.InstallCompletedActivity.java
com.android_mvc.sample_project.activities.installation.lib.InstallAppUserBaseActivity.java
com.android_mvc.sample_project.activities.main.TopActivity.java
com.android_mvc.sample_project.common.AppSettings.java
com.android_mvc.sample_project.common.Util.java
com.android_mvc.sample_project.controller.FuncDBController.java
com.android_mvc.sample_project.controller.FuncDBValidation.java
com.android_mvc.sample_project.controller.FuncNetController.java
com.android_mvc.sample_project.controller.FuncNetValidation.java
com.android_mvc.sample_project.controller.MainController.java
com.android_mvc.sample_project.controller.util.ValidationsUtil.java
com.android_mvc.sample_project.db.dao.FriendDAO.java
com.android_mvc.sample_project.db.dao.PrefDAO.java
com.android_mvc.sample_project.db.dao.todo.HogeDAO.java
com.android_mvc.sample_project.db.entity.Friend.java
com.android_mvc.sample_project.db.entity.lib.LogicalEntity.java
com.android_mvc.sample_project.db.schema.SchemaDefinition.java
com.android_mvc.sample_project.domain.DBDeleteAction.java
com.android_mvc.sample_project.domain.DBEditAction.java
com.android_mvc.sample_project.domain.DBUpdateAction.java
com.android_mvc.sample_project.domain.HttpNetAction.java