安卓app客户端访问php服务器数据交互
来源:互联网 发布:mysql命令启动 编辑:程序博客网 时间:2024/04/29 15:57
客户端使用httpclient与服务器数据交互是,类似在网页上提交一个表单,设定表单里各个input的名字,值,选择method为POST或GET,设置处理action的php文件,在安卓里也是类似的,把参数以键值对的形式存入一个参数列表,选择HTTP POST方法或者GET方法, 选择相应的类HttpPost或者HttpGet 并在构造方法传入php文件的url Php文件接受到请求后,会把参数放到一个超全局变量
在给出一个简单例子之前,先提醒一下,安卓xml配置文件中要加入INTERNET权限,而且httpclient调用相关逻辑代码不能在oncreate()方法里调用,要想调用一种解决方法是加代码强制执行,另一种是开启新的线程进行处理,本文采取后一种方式。
安卓客户端:
import java.util.ArrayList;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;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity { private String strResult=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.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() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText(strResult); } }); } private class RequestThread implements Runnable{ @Override public void run() { //因为选择POST方法,所以new HttpPost对象,构造方法传入处理请求php文件的url HttpPost httpRequest = new HttpPost("http://192.168.1.103/test/c2sdatademo.php"); //POST方法的参数列表 ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); //添加名为userName的参数,值为giantpoplar params.add(new BasicNameValuePair("userName", "haoxingxing")); 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; }}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>
权限
<uses-permission android:name="android.permission.INTERNET"/>
php服务器端
c2sdatademo.php
<?php //以参数名userName访问值 $user_name = $_POST['userName']; //echo 的结果会返回给安卓客户端,客户端程序里把下面echo //的内容赋值给了变量strResult echo "hello, ".$user_name."!"; ?>
源代码地址:http://download.csdn.net/detail/ruishaoma8471/9897932
阅读全文
0 0
- 安卓app客户端访问php服务器数据交互
- 安卓app客户端和使用php的服务器端数据交互
- 安卓app客户端和使用php的服务器端数据交互
- 安卓app客户端和使用php的服务器端数据交互
- app客户端与服务器数据交互时—数据加密
- 安卓APP客户端与服务器通信
- 用json实现服务器servlet和安卓客户端的数据交互
- 安卓服务器获取客户端数据
- 客户端和服务器数据交互
- Android客户端与服务器数据交互
- Android客户端和服务器数据交互
- GeoServer不同服务器安按装配置、数据发布及客户端访问
- 服务器采用JSON格式返回数据给安卓客户端
- android客户端与php服务器的json数据简单交互(一)
- android客户端与php服务器的json数据简单交互(二)
- android客户端与php服务器的json数据简单交互(一)
- php+ajax+xml在服务器和客户端之间多次交互数据
- php服务端接受安卓客户端的Json数据
- 《程序员的修炼——从优秀到卓越》读书笔记
- php对mongo写操作 遇到的类型转换问题
- IDEA部署多个web项目的方法
- App Store构建版本无效
- 关于Android写入文件失败的问题
- 安卓app客户端访问php服务器数据交互
- fragment懒加载问题
- MyBatis学习五:延迟加载
- WooCommerce——怎么修改产品列表页面中每个产品下面的Read More按钮上的文字
- Java String类的方法总结,附加使用。
- js动态向页面添加或删除控件
- AndroidStuido中报错提示Steup JDK是什么意思 ,如何 解决
- XSLT常见问题及解决(四) xsl中如何给属性值拼接字符串
- Android:可复制的TextView、EditText,复制文字到剪贴板