2016-04-05 5 views
2

ウェブサービスからdbレコードをダウンロードするアン​​ドロイドアプリを作成しました。スプラッシュ画面が表示されている間にこのダウンロード操作を実行します。私はすでにスプラッシュ画面を作成し、内部にダウンロードコードを書いていますが、私の問題はスプラッシュ画面がダウンロード終了まで表示されていないことです。 助けていただければ幸いです。ネットワークのダウンロード中にアンドロイドスプラッシュ画面を作成する

参考までに私は以下のスプラッシュスクリーンアクティビティクラスを含んでいます。

public class SplashScreen extends AppCompatActivity{ 

private static JSONObject jsonResponse; 
private static List<customerList> customerLists = new ArrayList<>(); 
private static List<productList> productLists = new ArrayList<>(); 
private static List<loginHistoryList> loginHistoryLists = new ArrayList<>(); 
private static List<salesOrderList> salesOrderLists = new ArrayList<>(); 
private static List<userList> userLists = new ArrayList<>(); 
public static Context mContext; 

private String TAG = getClass().getSimpleName(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 
    mContext = this; 

    if(ConnectivityChecker.CheckConnection(mContext)) { 
     ServerDataFetch mServerDataFetch = new ServerDataFetch(); 
     mServerDataFetch.execute(); 
    }else{ 
     Log.e(TAG,"There is no internet connection"); 
    } 
} 



/** 
* Created by abhilash on 4/3/2016. 
*/ 
private class ServerDataFetch extends AsyncTask<Void,Void,Void> { 

    UpdateLocalFeeds mUpdateLocalFeeds ; 

    public String TAG = getClass().getSimpleName(); 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      mUpdateLocalFeeds = new UpdateLocalFeeds(SplashScreen.mContext); 
      Log.i(TAG, "Streaming data from network: "); 
      downloadUrl(new VolleyCallback() { 
       @Override 
       public void onSuccessResponse(JSONObject result) { 
        try { 
         if (result != null) { 

          Log.d(TAG, result.get("customerList").toString()); 

          Log.i(TAG, "Parsing stream as Atom feed"); 

          Type listType = new TypeToken<Collection<customerList>>() { 
          }.getType(); 
          customerLists = new GsonBuilder().create().fromJson(result.get("customerList").toString(), listType); 
          Log.i(TAG, "Customers found " + customerLists.size()); 

          listType = new TypeToken<Collection<loginHistoryList>>() { 
          }.getType(); 
          loginHistoryLists = new GsonBuilder().create().fromJson(result.get("loginHistoryList").toString(), listType); 
          Log.i(TAG, "Login History found " + loginHistoryLists.size()); 

          listType = new TypeToken<Collection<productList>>() { 
          }.getType(); 
          productLists = new GsonBuilder().create().fromJson(result.get("productList").toString(), listType); 
          Log.i(TAG, "Product found " + productLists.size()); 

          listType = new TypeToken<Collection<salesOrderList>>() { 
          }.getType(); 
          salesOrderLists = new GsonBuilder().create().fromJson(result.get("salesOrderList").toString(), listType); 
          Log.i(TAG, "Sales found " + productLists.size()); 

          listType = new TypeToken<Collection<userList>>() { 
          }.getType(); 
          userLists = new GsonBuilder().create().fromJson(result.get("userList").toString(), listType); 
          Log.i(TAG, "Users found " + userLists.size()); 

          mUpdateLocalFeeds.updateLocalUserData(userLists); 
          mUpdateLocalFeeds.updateLocalCustomerData(customerLists); 
          mUpdateLocalFeeds.updateLocalProductData(productLists); 
          mUpdateLocalFeeds.updateLocalSalesData(salesOrderLists); 
          mUpdateLocalFeeds.updateLocalLoginData(loginHistoryLists); 
         } 

        } catch (Exception ex) { 
         Log.e(TAG, ex.getMessage()); 
        } 
       } 
      }); 
     }catch (Exception e){ 
      Log.e(TAG,e.getMessage()); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     Log.d(TAG,"Downloading completed"); 
     Intent intent = new Intent(SplashScreen.this, LoginActivity.class); 
     startActivity(intent); 
     finish(); 
    } 
    private void downloadUrl(final VolleyCallback volleyCallback) throws IOException { 

     final JsonObjectRequest jsObjRequest = new JsonObjectRequest 
       (Request.Method.GET, Constants.SERVER_DATA, new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, "Response: " + response.toString()); 

         jsonResponse = response; 
         volleyCallback.onSuccessResponse(response); 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         NetworkResponse networkResponse = error.networkResponse; 

         if (networkResponse != null) { 
          // HTTP Status Code: 401 Unauthorized 
          Log.e("SyncAdapter", networkResponse.statusCode+""); 
          Log.e("SyncAdapter", error.getMessage()); 
         } 
        } 
       }); 
     AppController.getInstance().addToRequestQueue(jsObjRequest,"JObject"); 
    } 
} 
interface VolleyCallback{ 
    void onSuccessResponse(JSONObject result); 
} 

}

答えて

0

表示

protected void onPreExecute() 

コールバック でのスプラッシュ画面と

protected void onPostExecute(String result) 
0

でそれを隠すだけで空白onPreExecutionを追加してみてください: -

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 
+0

違いがありますか? – abhilash

0

ダウンロードのコールバックがとてもonSuccessResponse

非同期の終わりにそれを移動呼ばれた後に意図が呼び出される必要がありdownloadUrl(new VolleyCallback()

を呼び出すときに別のスレッドを開始しているので、ここでは、バックグラウンドで実行するには無用ですあなたは、このようなexemple

のためのバッファとして制御することができます何かをして、独自のスレッドを使用していると、タスクが使用されている

private static JSONObject jsonResponse; 
private static List<customerList> customerLists = new ArrayList<>(); 
private static List<productList> productLists = new ArrayList<>(); 
private static List<loginHistoryList> loginHistoryLists = new ArrayList<>(); 
private static List<salesOrderList> salesOrderLists = new ArrayList<>(); 
private static List<userList> userLists = new ArrayList<>(); 
public static Context mContext; 

private String TAG = getClass().getSimpleName(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 
     mContext = this; 

     if(ConnectivityChecker.CheckConnection(mContext)) { 
     ServerDataFetch mServerDataFetch = new ServerDataFetch(); 
     mServerDataFetch.execute(); 
     }else{ 
     Log.e(TAG,"There is no internet connection"); 
     } 
     } 
private class ServerDataFetch extends AsyncTask<Void,Void,Void> { 

    UpdateLocalFeeds mUpdateLocalFeeds ; 

    public String TAG = getClass().getSimpleName(); 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      mUpdateLocalFeeds = new UpdateLocalFeeds(SplashScreen.mContext); 
      Log.i(TAG, "Streaming data from network: "); 
      downloadUrl(new VolleyCallback() { 
       @Override 
       public void onSuccessResponse(JSONObject result) { 
        try { 
         if (result != null) { 

          Log.d(TAG, result.get("customerList").toString()); 

          Log.i(TAG, "Parsing stream as Atom feed"); 

          Type listType = new TypeToken<Collection<customerList>>() { 
          }.getType(); 
          customerLists = new GsonBuilder().create().fromJson(result.get("customerList").toString(), listType); 
          Log.i(TAG, "Customers found " + customerLists.size()); 

          listType = new TypeToken<Collection<loginHistoryList>>() { 
          }.getType(); 
          loginHistoryLists = new GsonBuilder().create().fromJson(result.get("loginHistoryList").toString(), listType); 
          Log.i(TAG, "Login History found " + loginHistoryLists.size()); 

          listType = new TypeToken<Collection<productList>>() { 
          }.getType(); 
          productLists = new GsonBuilder().create().fromJson(result.get("productList").toString(), listType); 
          Log.i(TAG, "Product found " + productLists.size()); 

          listType = new TypeToken<Collection<salesOrderList>>() { 
          }.getType(); 
          salesOrderLists = new GsonBuilder().create().fromJson(result.get("salesOrderList").toString(), listType); 
          Log.i(TAG, "Sales found " + productLists.size()); 

          listType = new TypeToken<Collection<userList>>() { 
          }.getType(); 
          userLists = new GsonBuilder().create().fromJson(result.get("userList").toString(), listType); 
          Log.i(TAG, "Users found " + userLists.size()); 

          mUpdateLocalFeeds.updateLocalUserData(userLists); 
          mUpdateLocalFeeds.updateLocalCustomerData(customerLists); 
          mUpdateLocalFeeds.updateLocalProductData(productLists); 
          mUpdateLocalFeeds.updateLocalSalesData(salesOrderLists); 
          mUpdateLocalFeeds.updateLocalLoginData(loginHistoryLists); 

          Log.d(TAG,"Downloading completed"); 
          Intent intent = new Intent(SplashScreen.this, LoginActivity.class); 
          startActivity(intent); 
          finish(); 
         } 

        } catch (Exception ex) { 
         Log.e(TAG, ex.getMessage()); 
        } 
       } 
      }); 
     }catch (Exception e){ 
      Log.e(TAG,e.getMessage()); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

    } 
    private void downloadUrl(final VolleyCallback volleyCallback) throws IOException { 

     final JsonObjectRequest jsObjRequest = new JsonObjectRequest 
       (Request.Method.GET, Constants.SERVER_DATA, new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, "Response: " + response.toString()); 

         jsonResponse = response; 
         volleyCallback.onSuccessResponse(response); 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         NetworkResponse networkResponse = error.networkResponse; 

         if (networkResponse != null) { 
          // HTTP Status Code: 401 Unauthorized 
          Log.e("SyncAdapter", networkResponse.statusCode+""); 
          Log.e("SyncAdapter", error.getMessage()); 
         } 
        } 
       }); 
     AppController.getInstance().addToRequestQueue(jsObjRequest,"JObject"); 
    } 
} 
interface VolleyCallback{ 
    void onSuccessResponse(JSONObject result); 
} 
このコードを試してみてください
+0

downloadUrl関数を削除してコンテンツをdoInBackgroundに移動すると、違いはありますか? – abhilash

+0

はいこれは、ダウンロードスレッドに関する情報がないため、onSuccessがダウンロードが完了したときのonSuccessがon postExecuteではなくコールバックであるためです –

関連する問題