我使用AsyncTask功能创建了视频上传活动。我试图向服务器发送请求以上传视频,服务器需要更多时间上传并返回响应。反正有没有等待服务器响应发送响应服务器?我需要向服务器发送请求,无需等待repsonse并转移到其他页面。
请解释如何做。
先谢谢了。
答案 0 :(得分:0)
无需等待响应,不要在onPostExecute方法内启动活动(转移到其他页面)。
如果您发布代码,我可以为您提供更多帮助。
修改强>:
我不知道你是否真的想要这样做,用户如何知道视频已成功上传,你可以通过从(onPostExecute)移动开始活动代码来实现这一点,你可以把它放进去(onPreExecute),或者在调用(uploadVideo)之后,如下所示:
private String videourl;
private TextView statusView;
ProgressDialog uploading;
private String questionid;
private final int SPLASH_DISPLAY_LENGTH = 4500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
statusView = (TextView)findViewById(R.id.uploadstatus);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.containsKey("path")) {
videourl = extras.getString("path");
questionid = String.valueOf(extras.getInt("questionID"));
}
}
uploading = ProgressDialog.show(UploadActivity.this, "Uploading File", "Please wait...", false, false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
uploadVideo();
// add the start activity code here
Intent in = new Intent(getApplicationContext(), ViewQuestions.class);
// Intent in = new Intent(getApplicationContext(), DrawingActivity.class);
in.putExtra("questionID", questionid);
startActivity(in);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
private void uploadVideo() {
class UploadVideo extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
uploading.dismiss();
Log.i("Video Response", s.toString());
try {
JSONObject jsonObject = new JSONObject(s);
if(jsonObject.has("validation")){
if(jsonObject.getString("validation") == "true" ){
Toast.makeText(getApplicationContext(),"Video Created Successfully",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Video not created ",Toast.LENGTH_LONG).show();
}
//remove start activity call from here
/* Intent in = new Intent(getApplicationContext(), ViewQuestions.class);
// Intent in = new Intent(getApplicationContext(), DrawingActivity.class);
in.putExtra("questionID", questionid);
finish();
startActivity(in);*/
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... params) {
Upload u = new Upload();
String msg = u.uploadVideo(videourl,questionid,UploadActivity.this);
return msg;
}
}
UploadVideo uv = new UploadVideo();
uv.execute();
}
但我认为您应该使用Services而不是AsyncTask来确保上传完成,因为AsyncTask可能在活动结束后被杀死,请尝试this服务进行文件上传。
注意:强>
您应该将问题中的代码添加为答案
答案 1 :(得分:0)
公共类UploadActivity扩展了AppCompatActivity {
VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE);
}
请检查并告知我更改。 谢谢 。