AndroidStudio 中使用Xutils3注解

新建一个类继承Application  实现里面的onCreate方法


import org.xutils.x;

/**
 * Created by KeLei on 2016/2/2.
 */
public class LYZApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true); // 是否输出debug日志

    }
}
在AndroidManiFest中添加这个类


初始化成功



Fragment注解的方式


@ContentView(R.layout.fragment_first)
public class FirstFragment extends Fragment {
private MyAdapter adapter;
private List list = new ArrayList<>();
private List listUrl = new ArrayList<>();
private List listName = new ArrayList<>();
@ViewInject(R.id.btn_test)
Button btn_test;
@ViewInject(R.id.listView)
ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return x.view().inject(this, inflater, container);
}


ViewHolder注解的方式

public class MyAdapter extends BaseAdapter {
private Context context;
private List list;
private LayoutInflater mInflater;
private ImageOptions options;
public ViewHolder holder;

public MyAdapter(Context context, List list) {
this.context = context;
this.list = list;
this.mInflater = LayoutInflater.from(context);
options = new ImageOptions.Builder().setLoadingDrawableId(R.drawable.ic_launcher).setLoadingDrawableId(R.drawable.ic_launcher).setUseMemCache(true).setCircular(true).build();
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.itemone, null);
holder = new ViewHolder();
x.view().inject(holder, convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Person bean = list.get(position);
holder.tv_name.setText(bean.getName());
x.image().bind(holder.iv_image, bean.getImgUrl(), options);
return convertView;
}

class ViewHolder {
@ViewInject(R.id.tv_name)
private TextView tv_name;
@ViewInject(R.id.iv_image)
private ImageView iv_image;
}


XUtils的网络请求方法和一些网络请求框架的用法非常类似,我进行了一些简单的封装。 
封装一共有3个文件,分别是网络请求工具类XUtil,请求响应数据的解析类,还有一个就是一个请求成功的一个接口回调类。


public class XUtil {
/**
* 发送get请求 * @param
*/
public static Cancelable Get(String url, Map map, CommonCallback callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry entry : map.entrySet()) {
params.addQueryStringParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}

/**
* 发送post请求 * @param
*/
public static Cancelable Post(String url, Map map, CommonCallback callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry entry : map.entrySet()) {
params.addParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().post(params, callback);
return cancelable;
}

/**
* 上传文件 * @param
*/
public static Cancelable UpLoadFile(String url, Map map, CommonCallback callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry entry : map.entrySet()) {
params.addParameter(entry.getKey(), entry.getValue());
}
}
params.setMultipart(true);
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}

/**
* 下载文件 * @param
*/
public static Cancelable DownLoadFile(String url, String filepath, CommonCallback callback) {
RequestParams params = new RequestParams(url);//设置断点续传params.setAutoResume(true);params.setSaveFilePath(filepath);Cancelable cancelable = x.http().get(params, callback);return cancelable; }}



第二个类

public class JsonResponseParser implements ResponseParser {
//检查服务器返回的响应头信息
@Override
public void checkResponse(UriRequest request) throws Throwable { }
/** * 转换result为resultType类型的对象 * * @param resultType 返回值类型(可能带有泛型信息) * @param resultClass 返回值类型 * @param result字符串数据 * @return * @throws Throwable */
@Override
public Object parse(Type resultType, Class resultClass, String result) throws
Throwable {return new Gson().fromJson(result, resultClass); }}
}


第三个类
public class MyCallBack implements Callback.CommonCallback {
@Override
public void onSuccess(ResultType result) {
//可以根据公司的需求进行统一的请求成功的逻辑处理
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//可以根据公司的需求进行统一的请求网络失败的逻辑处理 }
@Override public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}}
}
}



示例1    get请求

//本get请求来自于免费测试接口:http://www.k780.com/api/entry.baiduString url="http://api.k780.com:88/?app=idcard.get";
Map map = new HashMap<>();
map.put("appkey","10003");
map.put("sign","b59bc3ef6191eb9f747dd4e83c99f2a4");
map.put("format","json");map.put("idcard","110101199001011114");
XUtil.Get(url,map,new MyCallBack(){
@Override
public void onSuccess(PersonInfoBean result)
{
super.onSuccess(result);
Log.e("result",result.toString());
}
@Override
public void onError(Throwable ex,boolean isOnCallback)
{super.onError(ex,isOnCallback); } } );


示例2  post请求



String url = "http://api.k780.com:88/?app=idcard.get";
Map map = new HashMap<>();
map.put("appkey","10003");
map.put("sign","b59bc3ef6191eb9f747dd4e83c99f2a4");
map.put("format","json");
map.put("idcard","110101199001011114");
XUtil.Post(url,map,new MyCallBack()
{
@Override
public void onSuccess(PersonInfoBean result)
{super.onSuccess(result);Log.e("result",result.toString());}
@Override
public void onError(Throwable ex,boolean isOnCallback){
super.onError(ex,isOnCallback);}});




原文链接为
http://www.th7.cn/Program/java/201512/740752.shtml  代码未格式化

你可能感兴趣的:(Android)