2015-08-19 61 views
0

我正在從我的電影項目中獲取電影海報從URL,然後把它們放到一個gridview。我使用asynctask來獲取JSON並解析json文件中的url。但是,當我啓動我的應用程序時,網格全部爲空,無法執行任何操作,直到旋轉屏幕或恢復我的應用程序。一旦我旋轉我的屏幕或恢復我的應用程序。它顯示所有圖片。我在我的代碼中刪除了我的api密鑰。Imageview沒有顯示,除非旋轉或恢復

public class FetchMovieTask extends AsyncTask<Void, Void, ArrayList<String>> { 
    private final String LOG_TAG = FetchMovieTask.class.getSimpleName(); 

    @Override 
    protected ArrayList<String> doInBackground(Void... params) { 
     String api_key = ""; 
     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 

     String movieJsonStr = null; 

     try { 
      Uri.Builder builder = new Uri.Builder(); 
      builder.scheme("http") 
        .authority("api.themoviedb.org") 
        .appendPath("3") 
        .appendPath("discover") 
        .appendPath("movie") 
        .appendQueryParameter("api_key", api_key); 
      String myUrl = builder.build().toString(); 

      URL Url = new URL(myUrl); 
      Log.v(LOG_TAG, myUrl); 
      // Create the request to OpenWeatherMap, and open the connection 
      urlConnection = (HttpURLConnection) Url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = urlConnection.getInputStream(); 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       // Nothing to do. 
       movieJsonStr = null; 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) 
       // But it does make debugging a *lot* easier if you print out the completed 
       // buffer for debugging. 
       buffer.append(line + "\n"); 
      } 

      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       movieJsonStr = null; 
      } 

      movieJsonStr = buffer.toString(); 

      Log.v("KPN", movieJsonStr); 

     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error ", e); 
      // If the code didn't successfully get the weather data, there's no point in attempting 
      // to parse it. 
      movieJsonStr = null; 
      return null; 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e(LOG_TAG, "Error closing stream", e); 
       } 

      } 
     } 
     try { 
      MovieURL = getPosterUrlFromJson(movieJsonStr); 
     } catch (JSONException e) { 
      Log.e(LOG_TAG, e.getMessage(), e); 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

private ArrayList<String> getPosterUrlFromJson(String forcastMovieStr) 
     throws JSONException { 

    final String OWM_PosterUrl = "poster_path"; 
    final String OWM_releaseDate = "release_date"; 
    final String OWM_overview = "overview"; 
    final String OWM_vote_average = "vote_average"; 
    final String OWM_original_title = "original_title"; 
    final String OWM_results = "results"; 


    JSONObject movieJson = new JSONObject(forcastMovieStr); 
    JSONArray movieArray = movieJson.getJSONArray(OWM_results); 

    ArrayList<String> resultStrs = new ArrayList<>(); 
    String posterurl = "http://image.tmdb.org/t/p/w185/"; 

    for (int i = 0; i < movieArray.length(); i++) { 
     // For now, using the format "Day, description, hi/low" 
     String title; 
     String overview; 
     String poster; 

     // Get the JSON object for movie poster 
     JSONObject moveposter = movieArray.getJSONObject(i); 
     poster = moveposter.getString(OWM_PosterUrl); 

     resultStrs.add(i,posterurl + poster); 

    } 
    return resultStrs; 
} 

protected void onPostExecute(ArrayList<String> strings) { 
    MovieURL.clear(); 
    for (String s : strings) { 
     MovieURL.add(s); 
    } 
} 

主營:

public class MainActivity extends AppCompatActivity { 
public static ArrayList<String> MovieURL = new ArrayList(); 
public static ImageListAdapter mImageListAdapter; 
public GridView gridview; 

@Override 
protected void onStart() { 
    super.onStart(); 
    new FetchMovieTask().execute(); 
    mImageListAdapter = new ImageListAdapter(this,MovieURL); 
    gridview.setAdapter(mImageListAdapter); 
    gridview.invalidateViews(); 
} 

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

    gridview = (GridView) findViewById(R.id.gridview); 


} 

private void updateMovieURL(){ 
    new FetchMovieTask().execute(); 
    gridview.invalidateViews(); 
} 

ImagelistAdapter類

public class ImageListAdapter extends ArrayAdapter { 
private Context context; 
private LayoutInflater inflater; 

private ArrayList<String> imageUrls; 

public ImageListAdapter(Context context, ArrayList <String> imageUrls) { 
    super(context, R.layout.image_view, imageUrls); 

    this.context = context; 
    this.imageUrls = imageUrls; 

    inflater = LayoutInflater.from(context); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (null == convertView) { 
     convertView = inflater.inflate(R.layout.image_view, parent, false); 
    } 

    Picasso.with(context).load(imageUrls.get(position)) 
      .placeholder(R.drawable.loading) 
      .into((ImageView) convertView); 

    return convertView; 
} 
} 

謝謝你們

回答

0

在postExecute做到這一點:

protected void onPostExecute(ArrayList<String> strings) { 
    MovieURL.clear(); 
    for (String s : strings) { 
     MovieURL.add(s); 
    } 
mImageListAdapter.notifyDataSetChanged(); 
} 
+0

不起作用.. =( –

0

我發現我的問題在asynctask上,我沒有重寫onPostExecute方法。我重寫我的onPostExecute後,我初始我的網格視圖

 @Override 
     protected void onPostExecute(ArrayList<String> strings) { 
     super.onPostExecute(strings); 
     mImageListAdapter = new ImageListAdapter(MainActivity.this, MovieURL); 
     gridview.setAdapter(mImageListAdapter); 
    }