android volley

实战 QQ demo源码(本例中有该应用)

服务器端下载http://download.csdn.net/download/knight_black_bob/9822551

android eclipse 版http://download.csdn.net/download/knight_black_bob/9822553

android stdio  版本http://download.csdn.net/download/knight_black_bob/9822556


android  volley
 
 

package com.example.demonetworkandimagecache;
 
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley; 
import com.example.demonetworkandimagecache.util.GsonRequest;
import com.example.demonetworkandimagecache.util.ImageCacheManager;
import com.google.gson.JsonElement;
public class MainActivity extends Activity {

	ImageView testView ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
	
		testView = (ImageView) this.findViewById(R.id.testView);
		


		
		/*
		 IResquest req = new IResquest() {
			
			@Override
			public void handler(String result) {
				try {
					JSONObject json = new JSONObject(result);
					String img =  (String) json.get("link");
					ImageCacheManager.loadImage(img, testView, getBitmapFromRes(R.drawable.ic_launcher), getBitmapFromRes(R.drawable.ic_launcher));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		};
		new RequestManager(req).stringRequestWithGet(url,new HashMap<String, String>());
		 */
		
		
		
//		String url = "http://10.2.6.60:8080/RepositoryPic/WebService/rest/user/login";
		/*GsonRequest<PicRepository> gsonRequest = new GsonRequest<PicRepository>(url, PicRepository.class,
				new Response.Listener<PicRepository>() {
					@Override
					public void onResponse(PicRepository response) {
						System.out.println("baoyou" + response.toString());
						Log.i("baoyou",response.toString());
						String img = response.getLink();
						ImageCacheManager.loadImage(img, testView, getBitmapFromRes(R.drawable.ic_launcher), getBitmapFromRes(R.drawable.ic_launcher));
						
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("baoyou",error.getMessage());

					}
				});
		Volley.newRequestQueue(MyApplication.newInstance()).add(gsonRequest); */
		
		
		//post 
		String url = "http://10.2.6.60:8080/RepositoryPic/WebService/rest/user/login";
		 
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userId", "1401155710");
		map.put("userPassword", "15155150208");  
		JSONObject jsonObject = new JSONObject(map);
		
		GsonRequest<QueryResultJson> gsonRequest = new GsonRequest<QueryResultJson>(
				Method.POST ,url, QueryResultJson.class, 
				jsonObject.toString(),
				
				new Response.Listener<QueryResultJson>() {
					@Override
					public void onResponse(QueryResultJson response) {
						JsonElement  obj =	response.retdata;
						  if (obj != null) {
							    /*{id=1.40115571E9,
									 name=baoy, 
									 nickName=呵呵,
									 password=15155150208,
									 mail=15155150208@163.com, 
									 telphone=15155150208, 
									 photo=http://10.2.6.60/users/1401155710/pics/1.png,
								     insertTime=2016-04-18,
									 lastUpdateTime=2016-04-15}*/
							  Log.e("baoyou", obj.toString());
							UserEntity  user = JsonParser.parseDateJson( obj.toString() , UserEntity.class);
							Log.e("baoyou", user.toString());
						 } 
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("baoyou", error.getMessage());

					}
				}) 
				/*{
			protected Map<String, String> getParams() {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("userName", "baoy");
				map.put("userPassword", "15155150208");
				return map;
			}
		}*/
		;
		Volley.newRequestQueue(MyApplication.newInstance()).add(gsonRequest); 
		
		//get
		String url2 = "http://localhost:8080/RepositoryPic/WebService/rest/friendShip/friendShips/1401155710";
		GsonRequest<QueryResultJson> gsonRequest2 = new GsonRequest<QueryResultJson>(url, QueryResultJson.class,
				new Response.Listener<QueryResultJson>() {
					@Override
					public void onResponse(QueryResultJson response) {
						System.out.println("baoyou" + response.toString());
						Log.i("baoyou",response.toString());
						Log.e("baoyou",response.retdata.toString()); 
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("baoyou",error.getMessage());

					}
				});
		Volley.newRequestQueue(MyApplication.newInstance()).add(gsonRequest);
		
	}
	 public Bitmap getBitmapFromRes(int resId) {
		          Resources res = this.getResources();
	         return BitmapFactory.decodeResource(res, resId);
	  
     }
	 
}

相关推荐