安卓app客户端和使用php的服务器端数据交互
2014-08-19 18:02
483 查看
php擅长网页开发,对http协议支持很好,如果采用php作为服务器的后台开发语言和安卓客户端进行数据交互,使封装了http协议的httpclient这个jar包,可以轻松进行数据交互,不需要了解http协议的过多细节。
Jar文件和api下载地址http://pan.baidu.com/s/1kTvCpAR
客户端使用httpclient与服务器数据交互是,类似在网页上提交一个表单,设定表单里各个input的名字,值,选择method为POST或GET,设置处理action的php文件,在安卓里也是类似的,把参数以键值对的形式存入一个参数列表,选择HTTP POST方法或者GET方法,
选择相应的类HttpPost或者HttpGet 并在构造方法传入php文件的url
Php文件接受到请求后,会把参数放到一个超全局变量 $_POST(请求用了POST方法)或者$_GET(请求用了GET方法)数组中,可以以参数名字为键访问参数的值。
在给出一个简单例子之前,先提醒一下,安卓xml配置文件中要加入INTERNET权限,而且httpclient调用相关逻辑代码不能在oncreate()方法里调用,要想调用一种解决方法是加代码强制执行,另一种是开启新的线程进行处理,本文采取后一种方式。
下面给出一个简单示例代码
安卓客户端
布局文件中添加button
配置文件中加入所需权限
php服务器端
c2sdatademo.php
先运行服务器,客户端再发送请求,我的测试结果如下
TextView默认文本是“Hello world!”,点击button后就能看到保存在strResult中的服务器返回的字符串
点击之前
点击按钮查看服务器返回的字符串的值
Jar文件和api下载地址http://pan.baidu.com/s/1kTvCpAR
客户端使用httpclient与服务器数据交互是,类似在网页上提交一个表单,设定表单里各个input的名字,值,选择method为POST或GET,设置处理action的php文件,在安卓里也是类似的,把参数以键值对的形式存入一个参数列表,选择HTTP POST方法或者GET方法,
选择相应的类HttpPost或者HttpGet 并在构造方法传入php文件的url
Php文件接受到请求后,会把参数放到一个超全局变量 $_POST(请求用了POST方法)或者$_GET(请求用了GET方法)数组中,可以以参数名字为键访问参数的值。
在给出一个简单例子之前,先提醒一下,安卓xml配置文件中要加入INTERNET权限,而且httpclient调用相关逻辑代码不能在oncreate()方法里调用,要想调用一种解决方法是加代码强制执行,另一种是开启新的线程进行处理,本文采取后一种方式。
下面给出一个简单示例代码
安卓客户端
package com.example.c2sdatademo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.example.c2sdatademo.R; import com.example.c2sdatademo.R.layout; //导入httpclient相关类 import org.apache.http.HttpResponse; 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.protocol.HTTP; import org.apache.http.util.EntityUtils; public class MainActivity extends Activity { private String strResult=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(layout.activity_main); //在新线程里发送请求并获得返回结果字符串,把值赋给strResult new Thread(new RequestThread()).start(); //点击按钮更新TextView的内容 final TextView tv = (TextView) findViewById(R.id.tv); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { tv.setText(strResult); } }); } private class RequestThread implements Runnable { @SuppressWarnings("unchecked") public void run() { //因为选择POST方法,所以new HttpPost对象,构造方法传入处理请求php文件的url HttpPost httpRequest = new HttpPost("http://localhost/test/c2sdatademo.php"); //POST方法的参数列表 ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); //添加名为userName的参数,值为giantpoplar params.add(new BasicNameValuePair("userName", "giantpoplar")); try { //设置请求实体,设定了参数列表 httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //执行请求,等待服务器返回结果 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); //log出http返回报文头 Log.e("status",httpResponse.getStatusLine().toString()); //判断返回码是否为200,200表示请求成功 if (httpResponse.getStatusLine().getStatusCode() == 200) { //获取返回字符串 strResult = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); } } catch (Exception e) { e.printStackTrace(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
布局文件中添加button
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Click" />
配置文件中加入所需权限
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
php服务器端
c2sdatademo.php
</pre></p><p><pre name="code" class="php"><?php //以参数名userName访问值 $user_name = $_POST['userName']; //echo 的结果会返回给安卓客户端,客户端程序里把下面echo //的内容赋值给了变量strResult echo "hello, ".$user_name."!"; ?>
先运行服务器,客户端再发送请求,我的测试结果如下
TextView默认文本是“Hello world!”,点击button后就能看到保存在strResult中的服务器返回的字符串
点击之前
点击按钮查看服务器返回的字符串的值
相关文章推荐
- 安卓app客户端和使用php的服务器端数据交互
- 安卓app客户端和使用php的服务器端数据交互
- 安卓app客户端访问php服务器数据交互
- 安卓客户端与mysql服务器端数据交互
- 服务器端与客户端使用Json进行数据交互
- 使用webservice实现App与服务器端数据交互
- 安卓客户端与服务器端交互(含文件上传、数据传输等)
- 使用Struts 2将客户端JSON数据映射为服务器端Java对象
- 安卓客户端与服务器端交互的工具类
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- js与php之间使用json格式进行数据交互
- 通俗易懂客户端与服务器端交互原理(HTTP数据请求与HTTP响应,包括Servlet部分)
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法:服务器端和客户端数据类型的自动转换:复杂类型
- Android客户端与服务器端的json数据交互(很详细)
- 浅谈B/S客户端与服务器端交互数据(二)
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 通俗易懂客户端与服务器端交互原理(HTTP数据请求与HTTP响应,包括Servlet部分
- 客户端与服务器端交互原理(HTTP数据请求与HTTP响应)