php结合安卓客户端实现查询交互实例

yipeiwu_com5年前PHP代码库

PHP 服务器端:

function getids()
{
  $this->output->set_header('Content-Type: application/json; charset=utf-8');
  $jsonstr = '';
  $pname = $pcallid = $pworkid = '';
 
  if (isset($_GET['name'])) {
    $pname = $_GET['name'];
  }
 
  if (isset($_GET['callid'])) {
    $pcallid = $_GET['callid'];
  }
 
  if (isset($_GET['workid'])) {
    $pworkid = $_GET['workid'];
  }
 
  $this->load->model('wireid_model');
 
  $this->wireid_model->insertonly($pname, $pcallid);
 
  if ($pname == '' && $pcallid == '' && $pworkid == '') {
    die();
  } else {
    $sqlstr = 'select * from twireid where 1=1 ';
    if ($pname != '') {
      $sqlstr = $sqlstr . " and GNAME='{$pname}' ";
    } else
      if ($pcallid != '') {
        $sqlstr = $sqlstr . " and GOLDCALLID='{$pcallid}' ";
      } else
        if ($pworkid != '') {
          $sqlstr = $sqlstr . " and GCARDID='{$pworkid}' ";
        }
    $getdata = $this->wireid_model->getsql($sqlstr);
    // JSON_FORCE_OBJECT 防止出现 []
    $jsonstr = json_encode($getdata->result_array(), JSON_FORCE_OBJECT);
    echo $jsonstr;
  }
}

  java 安卓端:

doAskTask = new Runnable() {
    @Override
    public void run() {
      // TODO
      // 在这里进行 http request.网络请求相关操作
      ggname = etname.getText().toString();
      ggworkid = etworkid.getText().toString();
      ggcallid = etcallid.getText().toString();
 
      String baseurl = ConfidDatas.askbaseurl;
      String askstr = "name=" + ggname + "&callid=" + ggcallid
          + "&workid=" + ggworkid;
      String result = null;
 
      HttpGet httpGet = new HttpGet(baseurl + askstr);
      // 第二步,使用execute方法发送HTTP GET请求,并返回HttpResponse对象
      HttpResponse httpResponse = null;
 
      try {
        httpResponse = new DefaultHttpClient().execute(httpGet);
      } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
 
      Message msg = new Message();
      Bundle data = new Bundle();
 
      if (httpResponse.getStatusLine().getStatusCode() == 200) {
        // 第三步,使用getEntity方法活得返回结果
        try {
          result = EntityUtils.toString(httpResponse.getEntity());
          data.putString("value", result);
          data.putString("result", "isok");
          msg.setData(data);
          handler.sendMessage(msg);
        } catch (ParseException e) {
          // e.printStackTrace();
        } catch (IOException e) {
          // e.printStackTrace();
        }
      } else { // 错误
        data.putString("value", "");
        data.putString("result", "iserr");
        msg.setData(data);
        handler.sendMessage(msg);
      }
    }
  };

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

实现PHP多线程异步请求的3种方法

在网上看过很多版本的PHP异步请求方法,这里简单总结几个常用方法分享给大家 1、用CURL实现一步请求 CURL扩展是我们在开发过程中最常用的一种方法,他是一个强大的HTTP命令行工具,...

php self,$this,const,static,->的使用

今天来总结下。 、在类的内部方法访问已经声明为const及static的属性时,使用self::$name的形式。注意的是const属性的申明格式,const PI=3.14,而不是co...

php中HTTP_REFERER函数用法实例

本文实例分析了php中HTTP_REFERER函数用法。分享给大家供大家参考。具体分析如下: 利用php的http_referer函数来判断用户的来路,这是比较简单的,实例代码如下: 复...

PHP带节点操作的无限分类实现方法详解

本文实例讲述了PHP带节点操作的无限分类实现方法。分享给大家供大家参考,具体如下: 包含(移动多个节点;移动单个节点;删除多个节点;删除单个节点;新增节点),另附数据库表结构 一、db...

如何用php获取程序执行的时间

在head.htm中加入,也就是在默认模版中添加“$stime=microtime(true); //获取程序开始执行的时间”复制代码 代码如下:<!--<?php$stim...