本文整理汇总了Java中com.zhy.http.okhttp.OkHttpUtils.post方法的典型用法代码示例。如果您正苦于以下问题:Java OkHttpUtils.post方法的具体用法?Java OkHttpUtils.post怎么用?Java OkHttpUtils.post使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.zhy.http.okhttp.OkHttpUtils
的用法示例。
在下文中一共展示了OkHttpUtils.post方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performRequestAsync
import com.zhy.http.okhttp.OkHttpUtils; //导入方法依赖的package包/类
@Override
public <T> void performRequestAsync(AyoRequest request, BaseResponseDispatcher responseHandler, BaseHttpCallback<T> callback) {
String url = request.url;
//基于OkHttpUtils辅助类
//1 method决定了OkHttpRequestBuilder的哪个子类
if(request.method.equalsIgnoreCase("get")){
OkHttpUtils
.get()
.headers(request.headers)
.url(url)
.tag(request.flag)
.build()
.execute(new MyStringCallback(request, responseHandler, callback));
}else if(request.method.equalsIgnoreCase("post")){
boolean hasStringEntity = (request.stringEntity != null && !request.stringEntity.equals(""));
boolean postFileLikeForm = (request.files != null && request.files.size() > 0);
boolean postFileLikeStream = (request.file != null);
//情况1:postForm
if(!hasStringEntity && !postFileLikeForm && !postFileLikeStream){
OkHttpUtils
.post()//
.url(url)
.headers(request.headers)
.params(request.params)
.tag(request.flag)
.build()
.execute(new MyStringCallback(request, responseHandler, callback));
}
//情况2:postString
//情况3:postFile--流形式,不带name,带mime
//情况4:postFile--表单形式,带name,带filename
if(hasStringEntity){
OkHttpUtils
.postString()
.url(url)
.headers(request.headers)
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.content(request.stringEntity)
.tag(request.flag)
.build()
.execute(new MyStringCallback(request, responseHandler, callback));
}else if(postFileLikeStream){
OkHttpUtils
.postFile()
.url(url)
.headers(request.headers)//
.file(request.file)
.build()
.execute(new MyStringCallback(request, responseHandler, callback));
}else if(postFileLikeForm){
PostFormBuilder b = OkHttpUtils.post();
for(String key: request.files.keySet()){
File f = request.files.get(key);
b.addFile(key, f.getName(), f);
}
b.url(url)//
.params(request.params)//
.headers(request.headers)//
.build()//
.execute(new MyStringCallback(request, responseHandler, callback));
}
}else{
throw new RuntimeException("使用了不支持的http谓词:" + request.method);
}
}
示例2: checkoutFromWeb
import com.zhy.http.okhttp.OkHttpUtils; //导入方法依赖的package包/类
/**
* 查询成绩
*/
private void checkoutFromWeb() {
//临时变量
tempXQ = "";
tempXN = "";
tempXQ = ddlXQ;
tempXN = ddlXN;
if (selectMode == null) {
return;
}
if (selectMode.equals(BUTTON_XQ)) {
//按学期查询
if (ddlXQ.equals("") || ddlXN.equals("")) {
return;
}
title = ddlXN + "学年第" + ddlXQ + "学期成绩";
} else if (selectMode.equals(BUTTON_ZX)) {
//在校成绩查询
title = "在校学习成绩";
ddlXQ = "";
ddlXN = "";
} else if (selectMode.equals(BUTTON_XN)) {
//按学年查询
if (ddlXN.equals("")) {
return;
}
ddlXQ = "";
title = ddlXN + "学年学习成绩";
}
if (!pbCjcx.isShown()) {
pbCjcx.setVisibility(View.VISIBLE);
}
final PostFormBuilder post = OkHttpUtils.post();
post.url(cjcxUrl)
.addHeader("Host", "210.44.159.4")
.addHeader("Referer", cjcxUrl)
.addParams("__VIEWSTATE", VIEWSTATE)
.addParams("__VIEWSTATEGENERATOR", "8963BEEC")
.addParams("ddlXN", ddlXN)
.addParams("ddlXQ", ddlXQ)
.addParams("txtQSCJ", "0")
.addParams("txtZZCJ", "100")
.addParams("Button1", "%B0%B4%D1%A7%C6%DA%B2%E9%D1%AF")
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
Toast.makeText(mContext, "查询失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(String response) {
HtmlUtils cjUtils = new HtmlUtils(response);
cjList = cjUtils.parseScore();
//如果CJList的size == 1,表示没有成绩;
if (cjList.size() == 1) {
title = "当前条件没有成绩哦";
}
//还原数据
ddlXQ = tempXQ;
ddlXN = tempXN;
initUI();
}
});
}
示例3: requestLogin
import com.zhy.http.okhttp.OkHttpUtils; //导入方法依赖的package包/类
/**
* 向服务器模拟登陆
*/
private void requestLogin() {
int visibility = tvError.getVisibility();
if (visibility == View.VISIBLE) {
tvError.setVisibility(View.INVISIBLE);
}
pbLogin.setVisibility(View.VISIBLE);
final PostFormBuilder post = OkHttpUtils.post();
post.url(loginUrl);
post.tag(this);
//下面数据抓包可以得到
post.addParams("__VIEWSTATE", "dDwtMTMxNjk0NzYxNTs7PpK7CYMIAY8gja8M8G8YpGL8ZEAL");
post.addParams("__VIEWSTATEGENERATOR", "92719903");
post.addParams("txtUserName", userId);
post.addParams("TextBox2", password);
post.addParams("txtSecretCode", code);
post.addParams("RadioButtonList1", "%D1%A7%C9%FA");
post.addParams("Button1", "");
post.addParams("lbLanguage", "");
post.addHeader("Host", "210.44.159.4");
post.addHeader("Referer", "//210.44.159.4/default2.aspx");
post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36");
post.build()
.connTimeOut(5000)
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
pbLogin.setVisibility(View.GONE);
tvError.setVisibility(View.VISIBLE);
}
@Override
public void onResponse(String response) {
View focusView = null;
if (response.contains("验证码不正确")) {
etCode.setError("验证码错误");
focusView = etCode;
} else if (response.contains("密码错误")) {
etPassword.setError("密码错误");
focusView = etPassword;
} else if (response.contains("用户名不存在")) {
etUsername.setError("用户名不存在");
focusView = etUsername;
}
if (focusView != null) {
focusView.requestFocus();
pbLogin.setVisibility(View.INVISIBLE);
//切换验证码
changCodeImage();
} else {
//登录成功
pbLogin.setVisibility(View.INVISIBLE);
//初始化用户数据
// initURL(response);
showSaveDataDialog(response);
}
}
});
}