picasso library.butを使用してデータを取得する画像アダプタを使用してグリッドビューで作成した画像ギャラリーでAndroidアプリを作成しようとしていますが、画像が表示されません。ピカソライブラリではなく、私はこの問題は、私はerror.everythingが正常に動作していないログ画像をアップロードするPicassoが画像を画像に読み込まない
**package com.example.android.popmovies;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* A placeholder fragment containing a simple view.
*/
public class PopMovieFragment extends Fragment {
ImageAdapter imageAdapter;
public PopMovieFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
imageAdapter = new ImageAdapter(getActivity(),new ArrayList<String>());
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("check","ok");
}
});
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
updateMovies();
}
public void updateMovies() {
FetchMovieTask movieTask = new FetchMovieTask();
movieTask.execute();
}
public class FetchMovieTask extends AsyncTask<String, Void, String[]> {
final String LOG_TAG = FetchMovieTask.class.getSimpleName();
private String[] getMovieImageFromJson(String movieJsonStr)
throws JSONException {
ArrayList<String> movieResults = new ArrayList<>();
String posterUrl;
final String OWM_MOVIE_RESULTS = "results";
JSONObject jsonObject = new JSONObject(movieJsonStr);
if (jsonObject.has(OWM_MOVIE_RESULTS)) {
JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);
//JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);
for (int i = 0; i < movieArray.length(); i++) {
JSONObject movieObject = movieArray.getJSONObject(i);
posterUrl = movieObject.getString("poster_path");
movieResults.add(posterUrl);
Log.v(LOG_TAG, posterUrl);
}
for (String items : movieResults){
Log.v(LOG_TAG,items);
}
// return null;
}
return movieResults.toArray(new String[0]);
// Log.v(LOG_TAG,posterUrl);
}
@Override
protected String[] doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String movieJsonStr = null;
//parameters for api link will be here
try {
final String MOVIE_BASE_URL = "https://api.themoviedb.org/3/movie";
final String APPID_PARAM = "api_key";
final String CRITERIA_FOR_MOVIE_SELECTION_TOP = "top_rated";
final String CRITERIA_FOR_MOVIE_SELECTION_POPULAR = "popular";
Uri builtUri = Uri.parse(MOVIE_BASE_URL).buildUpon()
.appendPath(CRITERIA_FOR_MOVIE_SELECTION_TOP)
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_MOVIEDB_API_KEY)
.build();
Log.v("TAG","BuiltURI" + builtUri.toString());
URL url = new URL(builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read input stream into a String
InputStream inputstream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputstream == null) {
movieJsonStr = null;
}
reader = new BufferedReader(new InputStreamReader(inputstream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
movieJsonStr = null;
}
movieJsonStr = buffer.toString();
Log.v(LOG_TAG, "Movie Json String" + movieJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "error here" + e);
movieJsonStr = null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "ErrorClosingStream", e);
}
}
}
try {
// here will be method call for JSon parsing.
String [] check_items = getMovieImageFromJson(movieJsonStr);
for (String items : check_items){
Log.v(LOG_TAG,items+"new");
}
return check_items;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String[] result) {
if (result != null) {
for (String resultItems : result){
Log.v(LOG_TAG,resultItems+"GoodLuck");
final String PICTURE_BASE_URL = "http://image.tmdb.org/t/p/w185/";
final String PICTURE_URL_END = resultItems;
Uri builtUri = Uri.parse(PICTURE_BASE_URL).buildUpon()
.appendPath(PICTURE_URL_END.replace("/",""))
.build();
Log.v(LOG_TAG,resultItems+"GoodLuck"+builtUri.toString());
imageAdapter.setmresultItems(builtUri.toString());
}
}
}
}
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mresultItems;
public ImageAdapter(Context c,ArrayList<String> resultItems){
mContext = c;
mresultItems = resultItems;
}
public int getCount(){
// return mThumbsIds.length;
return 0;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
public void setmresultItems(String resultItems){
mresultItems.add(resultItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null){
// if it is not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else{
imageView = (ImageView) convertView;
}
//imageView.setImageResource(mThumbsIds[position]);
for(String picResultUrl : mresultItems){
Picasso.with(mContext).load(picResultUrl).into(imageView);
}
return imageView;
}
//references to our Images
//private Integer[] mThumbsIds ;
}**
getView内でデバッグを行い、PicassoがpicResultUrlを正しく取得しているかどうかを確認します。また、.errorと.placeholderの画像を設定して、Picassoにエラーが発生しているかどうかを確認したり、画像データを取得したり、プレースホルダを表示したりすることはできません。 –
ピカソはエラーを出していません。ピカソに問題があることを意味します – Pheonix
次に、picResultUrlが間違っている可能性があります。デバッグを行います。 –