Android网络通信:通过volley和gson获取聚合数据

    由于工作原因,一直接触不到网络方面的知识,让我很苦恼。所以在闲暇时间,自己研究了下volley和gson,做了个小demo。存在博客上便于以后自己学习用到。

    首先,需要下载volley和gson的jar包导入到工程中,然后在聚合数据中获取一个菜谱数据的请求URL(这些简单步骤一看便会)。

    之后就是利用上面的资源来做了一个小demo,话不多说,上代码。


import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;


public class MainActivity extends Activity {

	private static final String TAG = "info";
	private RequestQueue mQueue;
	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mQueue = Volley.newRequestQueue(MainActivity.this);
		tv = (TextView) findViewById(R.id.tv);
		jsonGet();
	}

	private void jsonGet(){
		String url = "http://apis.juhe.cn/cook/category?key=0233c3a4ca3685cd3ee6efeef4e00481";
		StringRequest request = new StringRequest(Method.GET, url, 
				new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				for (int i = 0; i < getRecipe(response).size(); i++) {
					Log.i(TAG," parentId: " + getRecipe(response).get(i).getParentId() + " name: " + getRecipe(response).get(i).getName());
					for (int j = 0; j < getRecipe(response).get(i).getList().size(); j++) {
						List list = getRecipe(response).get(i).getList().get(j);
						Log.e(TAG, " id: " + list.getId() + " name: " + list.getName() + " parentId" + list.getParentId());
					}
				}
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
			}
		});
		mQueue.add(request);
	}

	private ArrayList<Result> getRecipe(String json){
		Gson gson = new Gson();
		Recipe recipe = gson.fromJson(json, Recipe.class);
		return recipe.getResult();
	}
}

    说说这边用到的库吧:

一.volley 

volley是Google推出的网络通信包,相比传统的网络请求方式有很大改进,非常的方便。这里我只用了3步就获取到了网络数据(用的是get方法获取),

1. 创建一个RequestQueue。

2. 创建一个StringRequest。

3. 将StringRequest添加到定义的RequestQueue中去。

哦,千万别忘了加上intent权限。

二.gson

gson是Google推出的一个用于处理json数据的一个包。

这里我使用了gson的fronJson方法,该方法有两个参数,第一个是json数据,第二个则是自己创建的对象类,对象类中的参数与json数据的key所对应。

我的Recipe.class代码如下:

import java.util.ArrayList;

public class Recipe {

	private int resultcode;
	private String reason;
	private ArrayList<Result> result;
	private int error_code;
	
	public int getResultcode() {
		return resultcode;
	}

	public void setResultcode(int resultcode) {
		this.resultcode = resultcode;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public ArrayList<Result> getResult() {
		return result;
	}

	public void setResult(ArrayList<Result> result) {
		this.result = result;
	}

	public int getError_code() {
		return error_code;
	}

	public void setError_code(int error_code) {
		this.error_code = error_code;
	}

}

json数据分两种,一种是简单的键值对,另一种则是一个key对应一个列表。如上面的result就要用Arraylist来存放。key对应数组的值可以使多个包含的,即数组中包含数组,数组的数组的数组中也可再包含数组,所以我这边建了3个class来存放json中的所有数据。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值