【发布时间】:2016-11-05 12:07:38
【问题描述】:
我有一个返回 StatusCode 的 FullRest API,我成功了,问题是,无法从 AsyncTask 调用进度对话框,我只想向用户“显示”发生了什么,if(statusCode == 201).. POST 成功,到目前为止:
PostBaseClass..
public class PostBase {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private String Url="http://192.168.0.101:3000/";
OkHttpClient client = new OkHttpClient();
int POST(final PostModel model) throws IOException{
Gson gson = new Gson();
String modelJson = gson.toJson(model);
RequestBody body = RequestBody.create(JSON,modelJson);
Request request = new Request.Builder()
.url(Url + "api/gone/POST")
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.code();
}
MainActivity..
public void MakePost(final PostModel postModel){
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
PostBase postBase = new PostBase();
statusCode = postBase.POST(postModel);
if(statusCode == 201){
//TODO
}else {
//TODO
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
【问题讨论】:
标签: java android android-asynctask progressdialog