首页 > 代码库 > xutil3 post 和 get请求

xutil3 post 和 get请求

https://i.cnblogs.com/EditPosts.aspx?postid=7001253

注册xutil3

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 实例化x,创建application,在oncreate方法中加入

 @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
    }

 这步千万别忘记了applicaiton声明,ApplicaitonData是我创建的applicaiton的名称

android:name=".ApplicaitonData"

activity oncreate代码

 x.view().inject(this);

 Fragment  oncreate代码

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

 

 

 

 

 

使用方法post请求

public void postData(){
        dialog.show();
        String url="http://route.showapi.com/138-46";

        RequestParams params = new RequestParams(url);
        params.addBodyParameter("showapi_appid","38170");
        params.addBodyParameter("showapi_sign","");
        params.addBodyParameter("prov","");



        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("+++++++++:onSuccess"+result);
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
            @Override
            public void one rror(Throwable throwable, boolean b) {
                System.out.println("+++++++++:onError:"+throwable);


                Message message = new Message();
                message.what = 2;
                handler.sendMessage(message);
            }

            @Override
            public void onCancelled(CancelledException e) {
                System.out.println("+++++++++:onCancelled");
            }

            @Override
            public void onFinished() {
                System.out.println("+++++++++:onFinished");
            }
        });
    }

 get请求

public void getData(){

        dialog.show();

        String url="http://route.showapi.com/138-46";
        RequestParams params = new RequestParams(url);
        params.addQueryStringParameter("showapi_appid","38170");
        params.addQueryStringParameter("showapi_sign","");
        params.addQueryStringParameter("prov","");




        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析result
                System.out.println("+++++++++:onSuccess"+result);
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
            //请求异常后的回调方法
            @Override
            public void one rror(Throwable ex, boolean isOnCallback) {
                System.out.println("+++++++++:ex:"+ex);
                Message message = new Message();
                message.what = 2;
                handler.sendMessage(message);
            }
            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
                System.out.println("+++++++++:onFinished:");
            }
        });
    }

 

Image使用

/*

        通过ImageOptions.Builder().set方法设置图片的属性
        ImageOptions imageOptions= new ImageOptions.Builder().setFadeIn(true).build(); //淡入效果
//ImageOptions.Builder()的一些其他属性:
        .setSquare(true) //设置图片显示为正方形
                .setCrop(true).setSize(200,200) //设置大小
                .setAnimation(animation) //设置动画
                .setFailureDrawable(Drawable failureDrawable) //设置加载失败的动画
                .setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的动画
        .setLoadingDrawable(Drawable loadingDrawable) //设置加载中的动画
                .setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的动画
        .setIgnoreGif(false) //忽略Gif图片
                .setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
                .setRaduis(int raduis) //设置拐角弧度
        .setUseMemCache(true) //设置使用MemCache,默认true
 */


        imageView = (ImageView)findViewById(R.id.imageView);

        ImageOptions imageOptions = new ImageOptions.Builder()
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setRadius(DensityUtil.dip2px(5))
                .setIgnoreGif(false)
                .setCrop(true)//是否对图片进行裁剪
                .setFailureDrawableId(R.drawable.ic_launcher)
                .setLoadingDrawableId(R.drawable.ic_launcher)
                .build();
        x.image().bind(imageView, "http://ytjr.gnway.cc:88//upload/YS01/img/4144148607704755a547da45dd15a473.png", imageOptions);

参考数据来源:http://blog.csdn.net/tyk9999tyk/article/details/53306035

 

xutil3 post 和 get请求