2016-09-07 5 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(); 
} 

}

チェックして、させて頂いて、問題のコードを追加する必要があります私は変化を知っている。 ありがとうございます。