2017-05-09 13 views
0

問題があります。アクティビティの中に私の非同期クラスがあります。 。私はonPostMethodに何も返しません。これをより一般的にするために、Factoryのようなデザインパターンを使用する必要がありますか?私のポイントは何ですか。私はデータでjsonを取得した後。私は "スピナー"を終了する必要がありますが、このProcccesDialogでは決してdoInBackGroundメソッドからデータを受け取ることはないため、終了しません。私はあなたのコードに問題があることを推測する任意のadvaiceAndroid AsyncTask Volley

private class ProgressTask extends AsyncTask <String, Void ,String>k{ 
    private ProgressDialog dialog = new ProgressDialog(LoginActivity.this); 

    @Override 
    protected void onPreExecute(){ 



     // ProgressDialog.show(LoginActivity.this, "text.."); 
     dialog=ProgressDialog.show(LoginActivity.this,"","Please Wait",false); 


    } 

    @Override 
    protected String doInBackground(String... args) { 
      RequestQueue queue = Volley.newRequestQueue(LoginActivity.this); 
     String url = args[0]; 
     Log.d("port", port); 
     Log.d("host", host); 
     Log.d("URL:", url); 
     final VolleyCallback callback = null; 


     final JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         String json = response.toString(); 
         ObjectMapper mapper = new ObjectMapper(); 
         try { 
          user = mapper.readValue(json, User.class); 

         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         if (user != null) { 

          Intent intent = new Intent(getBaseContext(), MainActivity.class); 
          intent.putExtra("host", host); 
          intent.putExtra("port", port); 
          intent.putExtra("appName", appName); 
          intent.putExtra("user", user); //zeby przekazac "implements Serializable 
          //User user =(User)getIntent().getSerializableExtra("user"); <-- żeby odebrać w 2 Activity 
          startActivity(intent); 

          if (user.getImie() != null) { 
           Toast.makeText(getApplicationContext(), "Witaj " + user.getImie(), Toast.LENGTH_SHORT).show(); 

          } 
         }else if (json == null){ 
          Toast.makeText(getApplicationContext(),"Błędny login lub hasło" , Toast.LENGTH_SHORT).show(); 
         } 
         Log.d("Response", response.toString()) 




        } 

       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.d("Host1991 ", String.valueOf(isHostReachable(host))); 
         if(user == null){ 
          Toast.makeText(getApplicationContext(), "Błędy login lub hasło", Toast.LENGTH_SHORT).show(); 
         }else if(!isHostReachable(host)){ 
          Toast.makeText(getApplicationContext(), , Toast.LENGTH_SHORT).show(); 
          Log.d("Error.Response", error.toString()); 

         } 


        } 

       } 


     ); 
     queue.add(getRequest); 
     return null; 

    } 

    @Override 
    protected void onPostExecute(final String success) { 
     // spinner.setVisibility(View.GONE); 


      dialog.dismiss(); 


    } 
+0

バックグラウンドスレッドで 'MainActivity'を起動しようとしているようです。それがログイン後にあなたのアプリの主要部分を起動するならば、それは良くありません。また、バックグラウンドスレッド中にUIを更新するには、 'publishProgress()'と 'onProgressUpdate()'を使います。 – Gary99

答えて

0

ためのおかげで、あなたが「DoInBackground()」メソッド、あなたの活動で自分の意思「OnPostMethod」にコンパイラに到達する前に変化を開始を呼び出しているということです。 私が提案できる最良の方法は、グローバルスコープでインテント変数を宣言し、 "OnPost"メソッドで "StartActivity(intent)"メソッド呼び出しを移動することです。 グローバルスコープ内のいくつかの変数を必要なものに初期化し、 "OnPost()"メソッドでそれらの変数にアクセスし、そこでインテントを呼び出します。

0

AsyncTaskを使用しないでください。

AsyncTaskのdoInBackgroundでVolleyコールを行う必要はありません。

間違ったデザイン。デザインが悪い。

0

「Volley」を使用している場合は、「Volley」自体がスレッドを管理するため、AsyncTaskや他のスレッド機構は必要ありません。ワーカースレッドでネットワーク操作を実行し、「メインスレッド(UIスレッド)」で応答します。