我已經使用AsyncTask函數創建了視頻上傳活動。我試圖發送請求切斷上傳視頻,服務器需要更多時間上傳並返回響應。無論如何,無需等待服務器響應就可以向服務器發送響應?只需要將請求發送到服務器,無需等待repsonse並轉移到其他頁面。Android視頻無需等待回覆
請解釋一下該怎麼做。
感謝提前。
我已經使用AsyncTask函數創建了視頻上傳活動。我試圖發送請求切斷上傳視頻,服務器需要更多時間上傳並返回響應。無論如何,無需等待服務器響應就可以向服務器發送響應?只需要將請求發送到服務器,無需等待repsonse並轉移到其他頁面。Android視頻無需等待回覆
請解釋一下該怎麼做。
感謝提前。
無需等待響應,不要啓動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();
}
而且我認爲你應該使用服務替代的AsyncTask以確保上傳將完成爲在活動完成後,AsyncTask可能會被終止,請嘗試this ser副文件上傳。
注:
你應該添加你的代碼中的問題沒有答案
公共類UploadActivity擴展AppCompatActivity {
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();
}
}, 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();
}
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();
}
}
請檢查並讓我知道變化。 謝謝。
非常感謝您的回覆。 – sathivel