android post 提交数据

简介: 引用:http://www.2cto.com/kf/201112/114551.html Android 实现Post向服务器提交数据  熟悉web编程的都很了解get和post这两种传递表单数据的方法。

引用:http://www.2cto.com/kf/201112/114551.html

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

 熟悉web编程的都很了解get和post这两种传递表单数据的方法。

 这里不具体介绍get和post的区别,如需了解请参考:http://www.2cto.com/kf/201112/114558.html

 所谓的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访问权限。

相关文章
|
3月前
|
JavaScript Java 数据安全/隐私保护
安卓逆向 -- POST数据解密
安卓逆向 -- POST数据解密
31 2
|
5月前
|
XML JSON Java
Android App网络通信中通过okhttp调用HTTP接口讲解及实战(包括GET、表单格式POST、JSON格式POST 附源码)
Android App网络通信中通过okhttp调用HTTP接口讲解及实战(包括GET、表单格式POST、JSON格式POST 附源码)
182 0
|
5月前
|
XML Java Android开发
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
102 0
|
5月前
|
XML Java Android开发
Android Studio App开发之网络通信中使用POST方式调用HTTP接口实现应用更新功能(附源码 超详细必看)
Android Studio App开发之网络通信中使用POST方式调用HTTP接口实现应用更新功能(附源码 超详细必看)
72 0
|
Android开发
uniapp 原生android插件实现get和post请求
uniapp 原生android插件实现get和post请求
289 0
uniapp 原生android插件实现get和post请求
|
Android开发
Android Retrofit 2.0框架 GET和POST的实现方式(配合RxJava)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/50608438 Android Retrofit 2.0框架 GET和POST的实现方式(配合RxJava)   简单说两句,该框架是okhttp再次封装的实现,性能挺高的哦。
1226 0
|
Android开发 网络协议 存储
Android客户端采用Http 协议Post方式请求与服务端进行数据交互
本示例以Servlet为例,演示Android与Servlet的通信。 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式。
1453 0
Android--httpclient模拟post请求和get请求
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/50964727 HttpClient的使用模式: 1.
910 0
|
Web App开发 消息中间件 Android开发
android 界面刷新 post send
引用:http://blog.csdn.net/tianshuai11/article/details/7428411 一,问题引入        异常: Only the original thread that created a view hierarchy can touch its v...
943 0
|
Java 测试技术 Android开发
android 网络 post get
引用:http://blog.csdn.net/zuolongsnail/article/details/6373051 Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。
896 0