我正在使用Retrofit2将Multipart数据发送到我的PHP文件。
Call<ServerResponse> call = getResponse.uploadFile(files,size);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call call1, Response response) {
ServerResponse serverResponse = (ServerResponse) response.body();
if (serverResponse != null) {
if(serverResponse.getMessage().equals("started")) {
Intent intent = new Intent(FileUpload.this, MainActivity.class);
startActivity(intent);
finishAffinity();
}
} else {
Toast.makeText(context,"ERROR",Toast.LENGTH_SHORT).show();
}
loadingDialog.dismissDialog();
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(context,"ERROR",Toast.LENGTH_SHORT).show();
loadingDialog.dismissDialog();
}
});
import com.google.gson.annotations.SerializedName;
public class ServerResponse {
@SerializedName("success")
boolean success;
@SerializedName("message")
String message;
public String getMessage() {
return message;
}
public boolean getSuccess() {
return success;
}
}
当PHP提供一个包含消息“ started”的json数组时,我需要启动MainActivity类。这是我的php代码
<?php
if(isset($_POST['size'])&&!empty($_FILES)) {
echo json_encode(array('success'=> 0, 'message'=>'started'));
for($x=0; $x < $_POST['size']; $x++) {
//CODE FOR VALIDATING THE FILES
}
}
?>
但这不会立即被调用,只有在执行了整个代码之后才被调用。我究竟做错了什么?请帮忙。问候。