2016-09-07 62 views
0

我已经使用AsyncTask函数创建了视频上传活动。我试图发送请求切断上传视频,服务器需要更多时间上传并返回响应。无论如何,无需等待服务器响应就可以向服务器发送响应?只需要将请求发送到服务器,无需等待repsonse并转移到其他页面。Android视频无需等待回复

请解释一下该怎么做。

感谢提前。

回答

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(); 
} 

而且我认为你应该使用服务替代的AsyncTask以确保上传将完成为在活动完成后,AsyncTask可能会被终止,请尝试this ser副文件上传。

注:

你应该添加你的代码中的问题没有答案

+0

非常感谢您的回复。 – sathivel

0

公共类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(); 
} 

}

请检查并让我知道变化。 谢谢。