我有一个返回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();
}