Android第九讲——网络(六)xUtils

xUtils
需要导包。xUtils-2.6.14.jar
.result是返回的结果 GET POST

public class XUtilsActivity extends AppCompatActivity implements View.OnClickListener {
    //使用注解去除findViewById
    @ViewInject(R.id.button_xutils_get)
    private Button mBtnXUtilsGet;
    @ViewInject(R.id.textview_xutils)
    private TextView mTextView;
    @ViewInject(R.id.button_xutils_post)
    private Button mBtnXUtilsPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xutils);

        ViewUtils.inject(this);//用注解去除findViewById后必须调用此方法
    }

    @OnClick({R.id.button_xutils_get, R.id.button_xutils_post})//注册点击事件(放在方法外边)

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_xutils_get:
                utilDoGet();
                break;
            case R.id.button_xutils_post:
                utilsDoPost();
                break;
            default:
                break;
        }
    }

    /**
     * 用Post方法提交服务器
     */
    private void utilsDoPost() {
        HttpUtils utils_Post = new HttpUtils();
        RequestParams params = new RequestParams();//添加提交参数
        params.addBodyParameter("username", "『唐』↹『人』");
        utils_Post.send(HttpRequest.HttpMethod.POST, "http://192.168.***.*:8080/HTTpTest_Servelet/ServeletTest", params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mTextView.setText(responseInfo.result);//responseInfo.result表示返回的结果
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mTextView.setText("网络连接错误");
            }
        });
    }

    /**
     * 用Get方法向服务器提交数据
     */
    private void utilDoGet() {
        HttpUtils utils_Get = new HttpUtils();
        utils_Get.send(HttpRequest.HttpMethod.GET, "http://192.168.***.*:8080/HTTpTest_Servelet/ServeletTest?username=Queue", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mTextView.setText(responseInfo.result);//responseInfo.result表示返回的结果
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mTextView.setText("网络连接错误");
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android xUtils3 是一个非常方便的 Android 开发工具包,其中包含了许多方便实用的功能,例如网络请求、图片加载等。下面是使用 xUtils3 获取网络图片的示例代码: 1. 在 build.gradle 中添加 xUtils3 的依赖: ```groovy dependencies { implementation 'org.xutils:xutils:3.5.0' } ``` 2. 在 AndroidManifest.xml 中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在代码中使用 xUtils3 获取网络图片: ```java ImageView imageView = findViewById(R.id.image_view); String imageUrl = "http://example.com/image.jpg"; x.image().bind(imageView, imageUrl, new Callback.CommonCallback<Drawable>() { @Override public void onSuccess(Drawable result) { // 图片加载成功 } @Override public void onError(Throwable ex, boolean isOnCallback) { // 图片加载失败 } @Override public void onCancelled(CancelledException cex) { // 图片加载被取消 } @Override public void onFinished() { // 图片加载完成 } }); ``` 上面的代码中,我们通过 x.image().bind() 方法来获取网络图片。第一个参数是 ImageView 对象,第二个参数是图片的 URL,第三个参数是一个 Callback,用于监听图片加载的状态。 其中,onSuccess() 方法表示图片加载成功,onError() 方法表示图片加载失败,onCancelled() 方法表示图片加载被取消,onFinished() 方法表示图片加载完成。在实际使用中,我们通常只需要实现 onSuccess() 方法即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值