2016-08-29 6 views
0

Async-TaskでWebサイトからデータを読み込みたい。
しかし、私は​​を呼び出しますが、Async-Taskには入りません。
preExecute()メソッドが呼び出されていなくても、少なくともそのようなログエントリは表示されません。なぜ私のAsyncTaskは実行されませんか?

public class NewFragment extends Fragment { 

    private String mResult; //This Result will be returned by FetchDataTask() 



    private final String LOG_TAG = NewFragment.class.getSimpleName(); 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 

    } 

    // fetch data from example.com and show them in textView 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     FetchDataTask fetchDataTask = new FetchDataTask(); 

     fetchDataTask.execute("http://example.com"); 


     View rootView = inflater.inflate(R.layout.fragment_new,container,false); 
     TextView textView = (TextView) rootView.findViewById(R.id.textView); 
     textView.setText(mResult); 


     return rootView; 


    } 

    public class FetchDataTask extends AsyncTask<String, Integer, String >{ 

     private final String LOG_TAG = FetchDataTask.class.getSimpleName(); 

     @Override 
     protected void onPreExecute(){ 
      Log.e(LOG_TAG,"onPreExecute()"); //This Log-entry doesn't appear, why?? 
     } 



     @Override 
     protected String doInBackground(String... strings){ 
      //http-connection: 
      HttpURLConnection httpURLConnection = null; 
      BufferedReader bufferedReader = null; 

      String result = ""; 

      try{ 
       URL url = new URL(strings[0]); 
       httpURLConnection = (HttpURLConnection) url.openConnection(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 

       //read data: 

       if(inputStream==null){ 
        Log.v(LOG_TAG,"There is nothin'"); 
       } else { 
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
        String line; 

        while ((line = bufferedReader.readLine()) != null) { 
         result += line + "\n"; 
        } 
       } 




      }catch(Exception e){ 
       Log.e(LOG_TAG, "Error in http connection"+e.toString()); 
      } finally { 
       if(httpURLConnection!=null){ 
        httpURLConnection.disconnect(); 
       } 
       if(bufferedReader != null){ 
        try{ 
         bufferedReader.close(); 
        }catch (final IOException e){ 
         Log.e(LOG_TAG,"Error closing stream", e); 
        } 
       } 
      } 



      Log.e(LOG_TAG,"doInBackground()"); 
      return result; 


     } 
     @Override 
     protected void onPostExecute(String string){ 
      mResult=null; 

      if(string!=null){ 
       mResult=string; 

      } 

     } 
    } 
} 
+0

を設定し、

private TextView textView; public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { super.onCreate(savedInstanceState); View rootView = inflater.inflate(R.layout.fragment_new,container,false); textView = (TextView) rootView.findViewById(R.id.textView); FetchDataTask fetchDataTask = new FetchDataTask(); fetchDataTask.execute("http://example.com"); return rootView; } 

そして後のフィールドにをのTextViewを保存し、あなたの断片を含む活動のコードを表示したり、チェックしましたonCreateView()が呼び出されましたか? –

+0

@KevinLEGOFF onCreateView()が働いています... – taxus1

+0

あなたのasynctaskの非実行とは無関係ですが、 'textView.setText(mResult);'は動作しません。 asynctaskが完了したら、あなたはそれをやっていません。タスクが開始されていなくても、あなたは即座にそれをやっています。 – njzk2

答えて

0

あなたはFragmentクラスを作ってアクティビティに追加していますか?それ以外の場合は、その問題を再現することはできません。

// This Result will be returned by FetchDataTask()

。それは非同期コードの仕組みではありません。 TextViewをnullに設定していて、AsyncTaskが終了するのを待っていません。

代わりに、単にテキスト

@Override 
protected void onPostExecute(String string){ 

    if(string!=null){ 
     textView.setText(string); 
    } 

} 
+0

ありがとう、本当に良い説明。たくさん助けて! – taxus1

+0

ようこそ。 AsyncTasksよりもうまく動作するので、Volley(JSONデータの場合はRetrofit)のような他のHTTPライブラリについて知ることをお勧めします。 –

-1

変更、public class FetchDataTask extends AsyncTask<String, Void, String>public class FetchDataTask extends AsyncTask<String, Integer, String>あなたasynctask実装に署名が同様にします。

また、setRetainInstance(true)をフラグメントonCreate()に追加すると、方向の変更が発生している間はビューを保持できます。ちょうどベストプラクティス。

そして最後に、あなたはあなたのような何かを持っているnewFragment.startFetching()を呼び出すので、活動のnewFragmentインスタンスを持たせ

public class NewFragment extends Fragment { 
    public void startFetching() { 
     FetchDataTask fetchDataTask = new FetchDataTask(); 
     fetchDataTask.execute("http://example.com"); 
    } 
    ... 
} 

参照:なぜあなたはメッセージが表示されないに関してhttps://gist.github.com/daichan4649/2480065

+0

何が違うのか説明できますか? – RafaelC

+1

ありがとう、良いアイデア!しかし残念ながら、それはまだ動作しません。 – taxus1

+0

署名を一致させるためには、IntegerパラメータでonProgressUpdate()を実装する必要があります。 –

関連する問題