def index() {        def parg=params        if(params.data){          //如果存在data参数,将该参数转换为JSONObject          JSONObject json=new JSONObject(params.data)            def sqlinsert=new Sql(dataSource)           //从JSONObject中获取数据并插入数据库            sqlinsert.executeInsert(" INSERT INTO person (name,gender,title) values (?,?,?)", [json.getString("name"),json.getString("gender"),json.getString("title")])        }        def sql= new Sql(dataSource)       //获取Person列表,以JSON方式返回        def rows=sql.rows("select name,gender,title from person")        sql.close()        render rows as JSON    }


 public void doHttpGetJSON(View view) throws IOException, JSONException {                DefaultHttpClient httpClient = new DefaultHttpClient();        //指定服务器端URL        HttpGet get = new HttpGet("http://10.4.30.228:8080/PersonForAndroid/person");        HttpResponse rsp = httpClient.execute(get);        //获取响应的实体        HttpEntity httpEntity = rsp.getEntity();        //将响应的实体转换为字符串        String jsonString = EntityUtils.toString(httpEntity);        //服务器端返回的数据格式为:[{"name":"Johnny","gender":"Male","title":"Programmer"},{"name":"Kevin","gender":"Male","title":"Manager"}]        //是一个JSON数组,因此使用JSONArray将字符串转换为JSONArray        //如果服务器端返回的是JSON字符串:{"name":"Johnny","gender":"Male","title":"Programmer"},则使用JSONObject jsonObject=new JSONObject(jsonString);        JSONArray jsonArray=new JSONArray(jsonString);        String resultsString="";        //遍历JSONArray,将结果输出        for (int i = 0; i < jsonArray.length(); i++) {            JSONObject jsonObj = jsonArray.getJSONObject(i);            String name = jsonObj.getString("name");            String gender = jsonObj.getString("gender");            String title = jsonObj.getString("title");            resultsString += title + " " + name + " is " + gender+"\r\n";        }        TextView getTextView = (TextView) findViewById(R.id.jsonGetTextView);        getTextView.setText(resultsString);    }


public void doHttpPostJSON(View view) throws IOException, JSONException {        //定义一个JSON,用于向服务器提交数据        JSONObject jsonObj = new JSONObject();        jsonObj.put("name", getTextFromView(R.id.name))                .put("gender", getTextFromView(R.id.gender))                .put("title", getTextFromView(R.id.title));        String jsonString = jsonObj.toString();        //指定Post参数        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);        nameValuePairs.add(new BasicNameValuePair("data", jsonString));                DefaultHttpClient httpClient = new DefaultHttpClient();        HttpPost post = new HttpPost("http://10.4.30.228:8080/PersonForAndroid/person");        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));        HttpResponse rsp = httpClient.execute(post);                HttpEntity httpEntity = rsp.getEntity();        String displayString = EntityUtils.toString(httpEntity);                TextView getTextView = (TextView) findViewById(R.id.jsonPostTextView);        getTextView.setText(displayString);    }


更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android(安卓)Settings中快速搜索流程
  3. Android获取本机电话号码的简单方法
  4. Android(安卓)Binder进程间通信-ServiceManager代理对象的获取过
  5. 修改android版本号
  6. Android获取本机Mac地址及IP地址的方法
  7. SQlite与android 的数据交互 (android 项目心得三)
  8. android 获取网络资源
  9. Android(安卓)根据坐标获取地址

随机推荐

  1. MySQL数据库基础命令大全(收藏)
  2. centos7安装mysql并jdbc测试教程
  3. 理解MySQL——索引与优化总结
  4. Mysql 忘记root密码的完美解决方法
  5. 详解mysql 获取当前日期及格式化
  6. MariaDB 新版本实力逆袭不仅仅是 MySQL
  7. 详解MySQL中concat函数的用法(连接字符串)
  8. MySQL(win7x64 5.7.16版本)下载、安装、配
  9. 在Linux环境下mysql的root密码忘记解决方
  10. mysql批量更新多条记录的同一个字段为不