xutils的http模块的简单使用

先导入xutils相关依赖:
compile 'org.xutils:xutils:3.5.0'

添加权限设置:



在清单文件中加入application,在自己的Application中添加初始化设置:
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
}

Activity请求实现:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private WebView wvInfo;
private Button btnRequest;

public static final String url = "https://www.baidu.com/";

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

findAndBind();

}

private void findAndBind(){
wvInfo = (WebView) findViewById(R.id.wv_info);
wvInfo.getSettings().setJavaScriptEnabled(true);
wvInfo.setWebChromeClient(new WebChromeClient());
btnRequest = (Button)findViewById(R.id.btn_request);
btnRequest.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_request:
httpRequest();
break ;
}
}

private void httpRequest(){
RequestParams params = new RequestParams(url);
// params.setSslSocketFactory(); // 设置ssl
// params.addQueryStringParameter("wd", "xUtils");

x.http().get(params, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
wvInfo.loadDataWithBaseURL(null,result, "text/html", "utf-8", null);
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}

@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}

@Override
public void onFinished() {

}
});
}
}

网页正确显示!

你可能感兴趣的:(android)