我有一個FullRest API,它返回一個StatusCode,並且我成功了,問題是無法從AsyncTask調用進度對話框,我只想「顯示」用戶發生了什麼,if(statusCode == 201)
..成功發佈,至今:如何在AsyncTask中創建進度對話框?
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();
}
太謝謝你了! –