请问如何使用JSON将web端访问的Mysql数据返回给android客户端

问题描述

请各位大神稍微讲解下方法,最好有点代码参考下哈

解决方案

你需要搜一下Gson,和apache httpclient下载导入对应的jar包web端: 首先,想办法把mysql取出的数据放到一个list中; 然后,参考这个帖子http://huyizizhen.iteye.com/blog/1453621 将list转成json 最后,返回给客户端out.println(json.toString());客户端: 需要一个httpclient String jsonstr = httpclient.get(url);
解决方案二:
通过 JSON RPC,这是本人前几天做的demo,应该满足你的需求,可直接运行java jsonrpc Server服务器 http://download.csdn.net/detail/zengxx1989/7310371Jsonrpc_android_Clienthttp://download.csdn.net/detail/zengxx1989/7310315
解决方案三:
给你点代码看吧package com.demo;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.BasicHttpParams;import org.apache.http.protocol.HTTP;import android.util.Log;public class WebDataGetApi { private static final String TAG = "WebDataGetAPI"; private static final String USER_AGENT = "Mozilla/4.5"; protected String getRequest(String url) throws Exception { return getRequest(url, new DefaultHttpClient(new BasicHttpParams())); } protected String getRequest(String url, DefaultHttpClient client) throws Exception { String result = null; int statusCode = 0; HttpGet getMethod = new HttpGet(url); Log.d(TAG, "do the getRequest,url=" + url + ""); try { getMethod.setHeader("User-Agent", USER_AGENT); // HttpParams params = new HttpParams(); // 添加用户密码验证信息 // client.getCredentialsProvider().setCredentials( // new AuthScope(null, -1), // new UsernamePasswordCredentials(mUsername, mPassword)); HttpResponse httpResponse = client.execute(getMethod); // statusCode == 200 正常 statusCode = httpResponse.getStatusLine().getStatusCode(); Log.d(TAG, "statuscode = " + statusCode); // 处理返回的httpResponse信息 result = retrieveInputStream(httpResponse.getEntity()); } catch (Exception e) { Log.e(TAG, e.getMessage()); throw new Exception(e); } finally { getMethod.abort(); } return result; } /** * 处理httpResponse信息,返回String * * @param httpEntity * @return String */ protected String retrieveInputStream(HttpEntity httpEntity) { int length = (int) httpEntity.getContentLength(); if (length < 0) length = 10000; StringBuffer stringBuffer = new StringBuffer(length); try { InputStreamReader inputStreamReader = new InputStreamReader( httpEntity.getContent(), HTTP.UTF_8); char buffer[] = new char[length]; int count; while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) { stringBuffer.append(buffer, 0, count); } } catch (UnsupportedEncodingException e) { Log.e(TAG, e.getMessage()); } catch (IllegalStateException e) { Log.e(TAG, e.getMessage()); } catch (IOException e) { Log.e(TAG, e.getMessage()); } return stringBuffer.toString(); }}建立JsonDataGetApi.javaimport org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class JsonDataGetApi extends WebDataGetApi { private static final String BASE_URL = "http://10.0.2.2:82/AccountService/"; private static final String EXTENSION = "Json/";; public JSONObject getObject(String sbj) throws JSONException, Exception { return new JSONObject(getRequest(BASE_URL + EXTENSION + sbj)); } public JSONArray getArray(String sbj) throws JSONException, Exception { return new JSONArray(getRequest(BASE_URL + EXTENSION + sbj)); }}我们的主Activity中调用刚才的方法, 在这一步中我们需要引入Google的gson 库gson-1.6.jarpublic class WebData extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getJsonData(); } public void getJsonData() { JsonDataGetApi api = new JsonDataGetApi(); JSONArray jArr; JSONObject jobj; try { //调用GetAccountData方法 jArr = api.getArray("GetAccountData"); //从返回的Account Array中取出第一个数据 jobj = jArr.getJSONObject(0); GsonBuilder gsonb = new GsonBuilder(); //Json中的日期表达方式没有办法直接转换成我们的Date类型, 因此需要单独注册一个Date的反序列化类. //DateDeserializer ds = new DateDeserializer(); //给GsonBuilder方法单独指定Date类型的反序列化方法 //gsonb.registerTypeAdapter(Date.class, ds); Gson gson = gsonb.create(); Account account = gson.fromJson(jobj.toString(), Account.class); Log.d("LOG_CAT", jobj.toString()); ((TextView) findViewById(R.id.Name)).setText(account.Name); ((TextView) findViewById(R.id.Age)).setText(account.Age); ((TextView) findViewById(R.id.Birthday)).setText(account.Birthday .toGMTString()); ((TextView) findViewById(R.id.Address)).setText(account.Address); } catch (Exception e) { Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); TextView movie_Address = (TextView) findViewById(R.id.Address); movie_Address.setText(e.getMessage()); } }}layout下的main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/Name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/Age" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/Birthday" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/Address" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>大致就是这么多
解决方案四:
首先你从数据库读取到一系列的对象,当然,也可以用Map来代替,然后把它们放在一个List中,然后再调用JSON库的序列化方法,使之成为一个json字符串,然后再写到客户端就可以了,这个时候客户端收到的就是一个JSONArray了。

时间: 2024-08-05 17:54:23

请问如何使用JSON将web端访问的Mysql数据返回给android客户端的相关文章

源代码-tomcat部署的web端服务器连接mysql,应用程序连接服务器给出的http接口

问题描述 tomcat部署的web端服务器连接mysql,应用程序连接服务器给出的http接口 5C 求救1.tomcat部署的web端服务器连接mysql,应用程序连接服务器给出的http接口,然后应用程序就可以通过和服务器交互来使用mysql里面的数据,这样理解是对的吗?2.如果这是对的,那服务器是不是就要封装好数据库连接类.数据增删查改类?这样,应用程序就可以只需要传参数就可以使用数据了?3.在服务器端封装好数据库连接类的话:那不是每次应用程序使用数据的时候,服务器都要连接又断开数据库了吗

服务器-通过JSON格式获取到的html页面,怎么在Android客户端显示

问题描述 通过JSON格式获取到的html页面,怎么在Android客户端显示 服务器端直接将一个有图片有超链接的html封装成JSON中的一个字段的值,客户端获取后怎么去显示这个html呢?通过setText(Html.fromHtml(map.get(""content"")))明显是不可以的,因为还是有图片和链接的.JSON如下: 解决方案 http://blog.sina.com.cn/s/blog_75016706010149yy.html 解决方案二:

php访问查询mysql数据的三种方法

1. $row = mysql_fetch_row($result); 返回一个规则的数组$row,$row[0]是第一个元素,$row[1]是第二个元素,依次类推... mysql_num_fields($result) 返回结果的元素个数. 2. $row = mysql_fetch_array($result); 返回一个数组$row. 举例如下: 表结构如下: username | password ------------------------------------- bourbo

php访问查询mysql数据的三种方法_php基础

1. $row = mysql_fetch_row($result); 返回一个规则的数组$row,$row[0]是第一个元素,$row[1]是第二个元素,依次类推... mysql_num_fields($result) 返回结果的元素个数. 2. $row = mysql_fetch_array($result); 返回一个数组$row. 举例如下: 表结构如下: username | password ------------------------------------- bourbo

C#进行Visio二次开发之Web端启动绘图客户端并登录

有这样的需求,一个系统,包含Web端的后台和Winform的绘图客户端程序,用户需要在Web端能够启动绘图客户端,并且不需要重新登录(因为已经登录了Web端了). 那么在IE的Web端,如何启动Winform做的绘图客户端程序呢?当然对于其他桌面应用程序也是一样的. 总体思路是: 1. 在asp.net页面中增加一个按钮或者菜单,连接是调用一个JavaScript函数实现启动程序 2. 客户端的用户的环境变量有该应用程序的目录路径信息 3. Winform的绘图客户端程序能够处理传递过来的命令行

实现一个移动端访问数据库的webservice接口的流程

问题描述 实现一个移动端访问数据库的webservice接口的流程,过程中要用到哪些东东,最好有实例... 解决方案 解决方案二:用C#这个语言解决方案三:提供一个例子:Android客户端调用C#WebService.http://blog.csdn.net/chinacsharper/article/details/38386779 解决方案四:没移动端部分,只有服务端部分解决方案五:这不就是webservice么..VS新建网站添加一个web服务写方法就搞定了啊..至于客户端(调用者)是c

js如何判断用户是在PC端和还是移动端访问_javascript技巧

最近一直在忙我们团队的项目"咖啡之翼",在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而且许多国内开发者也开始投身于Android移动终端的大浪潮中.如果很多互联网大浪潮你错过了.那么这个Android浪潮你绝对不能错过.目前我们为"咖啡之翼"已经开发了移动终端以及安卓客户端,大家使用Android或者IOS操作系统的手机,直接访问域名www.sygxy.cn即可观看移动终端效果.同时Android客户

【智能合约】客户端和web端对智能合约的事件Event进行调用的代码示例

客户端和web端对智能合约的事件Event进行调用的代码示例 web truffle 按官网的例子 http://truffleframework.com/boxes/pet-shop truffle作为一个运行测试框架,用的也是web3对智能合约进行调用. 文件所在的位置src/js/app.js initWeb3: function() { // web3入口 if (typeof web3 !== 'undefined') { App.web3Provider = web3.current

文件下载-手机浏览器从web端下载文件失败,但是在pc浏览器上正常,该如何解决?

问题描述 手机浏览器从web端下载文件失败,但是在pc浏览器上正常,该如何解决? 这个是抛出的异常 org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393) at org.apache.tomcat.util.buf.B