2015-12-19 1 views
1

J'essaie d'utiliser HttpResponseCache pour mettre en cache les données du réseau, mais quand je mets installer le code de cache OnCreate et OnPause méthodes, application se bloque. J'ai utilisé le code HttpResponseCache documentation. et les erreurs de compilation apparaissent comme indiqué ci-dessous. après avoir utiliséL'utilisation HttpResponseCache fait crash d'application

@TargetApi (Build.VERSION_CODES.ICE_CREAM_SANDWICH)

pour résoudre les erreurs disparaître, je lance l'application, mais encore se bloque. lorsque je supprime ce code, il revient à son comportement normal. Je suis un apprenant débutant et je serai reconnaissant si quelqu'un peut vous aider. voici le code MainActivity

public class MainActivity extends AppCompatActivity { 


    private MovieAdapter movieAdapter; 
    private Context context; 



    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     // ------ caching data ------------------ 
     try { 
      File httpCacheDir = new File(context.getCacheDir(), "http"); 
      long httpCacheSize = 2 * 1024 * 1024; // 10 MiB 
      HttpResponseCache.install(httpCacheDir, httpCacheSize); //error 
     } catch (IOException e) { 
      Log.i("caching failed", "HTTP response cache installation failed:" + e); 
     } 

     //----------------------- 

     DataSyncTask dataSyncTask = new DataSyncTask(); 
     dataSyncTask.execute(); 
    } 


     @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_main, menu); 
     return true; 
    } 



    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if (id == R.id.action_settings) { 
      startActivity(new Intent(this, SettingsActivity.class)); 

      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    private class DataSyncTask extends AsyncTask<Void, Void, ArrayList<Movie>> { 

     @Override 
     protected ArrayList<Movie> doInBackground(Void... params) { 
      JSONHttpClient jsonHttpClient = new JSONHttpClient(MainActivity.this); 
      JSONDataParser finalData = new JSONDataParser(); 
      /* 
      using shared preference to obtain data 
      */ 
      SharedPreferences sharedPreferences = 
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

      String sortingOrder = sharedPreferences.getString(
        getString(R.string.pref_findMovies_key), 
        getString(R.string.pref_popular_value)); 

      String streamData = jsonHttpClient.getData(sortingOrder); 
      return finalData.getParsedData(streamData); 

     } 

     @Override 
     protected void onPostExecute(final ArrayList<Movie> movies) { 


      movieAdapter = new MovieAdapter(getApplicationContext(), movies); 
      GridView gridView = (GridView) findViewById(R.id.movies_grid); 
      gridView.setAdapter(movieAdapter); 
      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
        Movie movie = (Movie) adapterView.getItemAtPosition(i); 
        Intent intent = new Intent(MainActivity.this, DetailActivity.class); 
        intent.putExtra("MovieTitle", movie.getTitle()); 
        intent.putExtra("voteAverage", movie.getVoteAverage()); 
        intent.putExtra("ReleaseDate", movie.getReleaseDate()); 
        intent.putExtra("MovieOverview", movie.getOverview()); 
        intent.putExtra("PosterPath", movie.getPosterPath()); 

        startActivity(intent); 
       } 
      }); 

     } 
    } 

    @Override 
    protected void onResume() { 
     DataSyncTask dataSyncTask = new DataSyncTask(); 
     dataSyncTask.execute(); 
     super.onResume(); 

    } 

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
    @Override 
    protected void onStop() { 
     HttpResponseCache cache = HttpResponseCache.getInstalled(); 
     if (cache != null) { 
      cache.flush(); 
     } 
     super.onStop(); 
    } 
} 

que ce qui apparaît dans logcat après un crash de l'application

12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested 
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14585: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode 
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14589: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
12-19 19:22:58.505 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
12-19 19:22:58.505 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 451: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
12-19 19:22:58.515 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
12-19 19:22:58.515 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 473: Landroid/content/res/TypedArray;.getType (I)I 
12-19 19:22:58.585 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415ffc80) 
+0

Où sont les messages d'erreur? –

+0

@Orlangure Je viens de l'ajouter, espérons que clair quel est le problème –

Répondre

0

Selon HttpResponseCache Docs:

Activer la mise en cache de toutes les requêtes HTTP de votre application par installer le cache au démarrage de l'application.

vous devez donc utiliser comme suit:

MyApplication.java:

public class MyApplication extends Application { 

    private final Context mContext = this; 

    public void onCreate() { 
     super.onCreate(); 

     // Install the cache 
     try { 
      File httpCacheDir = new File(mContext.getCacheDir(), "http"); 
      long httpCacheSize = 10 * 1024 * 1024; // 10 MiB 
      HttpResponseCache.install(httpCacheDir, httpCacheSize); 
     } catch (IOException e) { 
      Log.i("CacheHttpUrlConnection", "HTTP response cache installation failed:" + e); 
     } 

     // Register for activity lifecycle callbacks, 
     // specifically interested in activity stop callbacks. 
     registerActivityLifecycleCallbacks(
       new MyApplicationActivityLifeCycleCallbacks()); 
    } 

    // method to flush cache contents to the filesystem 
    public void flushCache() { 
     HttpResponseCache cache = HttpResponseCache.getInstalled(); 
     if (cache != null) { 
      cache.flush(); 
     } 
    } 

    private class MyApplicationActivityLifeCycleCallbacks implements ActivityLifecycleCallbacks { 
     @Override 
     public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 
     } 

     @Override 
     public void onActivityStarted(Activity activity) { 
     } 

     @Override 
     public void onActivityResumed(Activity activity) { 
     } 

     @Override 
     public void onActivityPaused(Activity activity) { 
     } 

     public void onActivityStopped(Activity activity) { 
      flushCache(); 
     } 

     @Override 
     public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 
     } 

     @Override 
     public void onActivityDestroyed(Activity activity) { 
     } 
    } 
} 

MainActivity.java:

intérieur doInBackground de AsyncTask Classe:

... 
URL url = new URL(address); 
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale 
urlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale); 
// Checks cache 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
    cache = HttpResponseCache.getInstalled(); 
    if (cache != null) { 
     cacheInfo = "Request count: " 
       + cache.getRequestCount() + ", hit count " 
       + cache.getHitCount() + ", network count " 
       + cache.getNetworkCount() + " size = " 
       + cache.size() + " <-----------------"; 
     Log.i(LOG_TAG, cacheInfo); 
    } 
} 
// Gets stream 
Log.i(LOG_TAG, "RespCode: " + urlConnection.getResponseCode()); 
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { 
    inputStream = urlConnection.getInputStream(); 
} else { 
    inputStream = urlConnection.getErrorStream(); 
} 
// Parses stream to string 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
String temp, response = ""; 
while ((temp = bufferedReader.readLine()) != null) { 
    response += temp; 
} 
... 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.cachehttpurlconnection" > 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     android:name="com.example.cachehttpurlconnection.MyApplication" > 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest>