请问如何使用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);

首先你从数据库读取到一系列的对象,当然,也可以用Map来代替,然后把它们放在一个List中,然后再调用JSON库的序列化方法,使之成为一个json字符串,然后再写到客户端就可以了,这个时候客户端收到的就是一个JSONArray了。

[code="java"]
给你点代码看吧
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.java
import 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.jar

public 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"?>
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content" />
android:layout_height="wrap_content" />
android:layout_height="wrap_content" />
android:layout_height="wrap_content" />

大致就是这么多
[/code]

通过 JSON RPC,这是本人前几天做的demo,应该满足你的需求,可直接运行

java jsonrpc Server服务器
[url]http://download.csdn.net/detail/zengxx1989/7310371[/url]

Jsonrpc_android_Client
[url]http://download.csdn.net/detail/zengxx1989/7310315[/url]