Android 实现Post向服务器提交数据

本文介绍了Android应用如何通过POST方式向服务器提交数据,并提供了一个完整的示例代码。该示例展示了如何设置请求参数、执行POST请求及处理响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 实现Post向服务器提交数据
熟悉web编程的都很了解get和post这两种传递表单数据的方法。
这里不具体介绍get和post的区别,如需了解请参考。http://www.cnblogs.com/wxf0701/archive/2008/08/17/1269798.html<!--more-->
所谓的get传递数据也是我们最常见的一种,如http://127.0.0.1/index.php?param=androidyue,这种方式直接显示在url中,因此很不安全,
而使用post传递数据则不会直接暴露出来,相对来说更加安全一些。post传递也需要key和value。
以下是android程序示例代码:
package com.google.code.cakedroid.demo;




import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


import com.google.code.cakedroid.R;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class PostMethodDemoActivity extends Activity{
	//declare the variables
	private TextView tvResult;
	private Button btnClick;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.initViews();
	}
	
	/*
	 * initialize necessary views
	 */
	private void initViews(){
		this.initButtons();
		this.initTextViews();
	}
	
	/*
	 * initialize necessary textviews
	 */
	private void initTextViews(){
		this.tvResult=(TextView)this.findViewById(R.id.tvResult);
	}
	
	/*
	 * initialize necessary buttons
	 */
	private void initButtons(){
		this.btnClick=(Button)this.findViewById(R.id.btnClick);
		this.btnClick.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				postData();
			}
		});
	}
	
	/*
	 * post data to remote host
	 */
	private void postData(){
		String destUrl="http://10.0.2.2/form_handler.php";
		//instantiate HttpPost object from the url address
		HttpEntityEnclosingRequestBase httpRequest =new HttpPost(destUrl);
	    //the post name and value must be used as NameValuePair
	    List <NameValuePair> params=new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("param","I have posted you the data"));
	    try{
	     httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
	     //execute the post and get the response from servers
	     HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
	      
	     if(httpResponse.getStatusLine().getStatusCode()==200){
	      //get the result
	      String strResult=EntityUtils.toString(httpResponse.getEntity());
	      tvResult.setText(strResult);
	     }else{
	      tvResult.setText("Error Response"+httpResponse.getStatusLine().toString());
	     }
	    }catch(Exception e){
	    	System.out.println("error occurs");
	    }
	}
}


服务器断php代码:
<?php
	if(isset($_POST['param'])){
		echo $_POST['param'].'  I received the data';
	}
?>


注意:如果如需正常访问,请在manifest.xml中添加internet访问权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值