android volley
16lz
2021-01-23
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 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);testView = (ImageView) this.findViewById(R.id.testView);/* IResquest req = new IResquest() {@Overridepublic 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 blocke.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>() {@Overridepublic 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() {@Overridepublic 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>() {@Overridepublic 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() {@Overridepublic 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); //getString url2 = "http://localhost:8080/RepositoryPic/WebService/rest/friendShip/friendShips/1401155710";GsonRequest<QueryResultJson> gsonRequest2 = new GsonRequest<QueryResultJson>(url, QueryResultJson.class,new Response.Listener<QueryResultJson>() {@Overridepublic void onResponse(QueryResultJson response) {System.out.println("baoyou" + response.toString());Log.i("baoyou",response.toString());Log.e("baoyou",response.retdata.toString()); }}, new Response.ErrorListener() {@Overridepublic 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); } }
更多相关文章
- android 图片背景使用这种方式很爽
- android获得圆角图片
- Android在Button按钮上同时显示文字和图片
- android ViewFlipper实现图片轮播
- Android如何使用XML创建一个环形渐变颜色图片
- Android 旋转图片
- Android 获取网络图片
- Android 图片加载缓存
- Android 系统图片