我正在开发从TMDB.com获取电影数据的APP
进入gridview。所以问题是doInBackground
方法没有执行。它没有显示任何LOG消息,我只是显示空白屏幕没有错误
我尝试使用onPostExecute
方法在OnCreateView
中设置适配器,但没有改变。
ASYNCTASK SUBCLASS
public class fetchmovies extends AsyncTask<String, Void, String[]> {
private final String LOG_TAG = fetchmovies.class.getSimpleName();
@Override
protected String[] doInBackground(String... params) {
if (params.length == 0) {
return null;
}
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String moviejasonSTR = null;
String size = "w185";
String poster_path = "poster_path";
String api_key = "my api key";
try {
final String base_move_url = " http://image.tmdb.org/t/p/";
final String SORT_BY_PARM = "sort_by";
final String Api_KEY_PARM = "api_key";
Uri bulitUri = Uri.parse(base_move_url).buildUpon()
.appendQueryParameter(SORT_BY_PARM, params[0])
.appendQueryParameter(Api_KEY_PARM, api_key).build();
URL url = new URL(bulitUri.toString());
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.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
moviejasonSTR = buffer.toString();
Log.v(LOG_TAG, "movie string: " + moviejasonSTR);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
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 {
return getMoviesDatFromJason(moviejasonSTR);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
}
return null;
}
private String[] getMoviesDatFromJason(String movieJasonStr) throws JSONException {
final String RESULT = "results";
// final String OVERVIEW = "overview";
//final String ORIGINAL_TITLE = "original_title";
//final String VOTE_AVERAGE = "vote_average";
final String POSTER_PATH = "poster_path";
// final String RELEASE_DATE = "release_date";
JSONObject movieJason = new JSONObject(movieJasonStr);
JSONArray MOVIEARAY = movieJason.getJSONArray(RESULT);
String[] imagearry = new String[MOVIEARAY.length()];
for (int i = 0; i < MOVIEARAY.length(); i++) {
JSONObject movie = MOVIEARAY.getJSONObject(i);
String poster_path = movie.getString(POSTER_PATH);
String poster_uril = "http://image.tmdb.org/t/p/w185/" + poster_path;
imagearry[i] = poster_uril;
}
return imagearry;
}
@Override
protected void onPostExecute(String[] result) {
}
}
BASE ADAPTER
public class mcustomAdapter extends BaseAdapter {
private Context context;
private String[] posterURL;
public mcustomAdapter(Context context, String[] posterUrl) {
this.context = context;
this.posterURL = posterUrl;
}
@Override
public int getCount() {
return posterURL.length;
}
@Override
public Object getItem(int position) {
return posterURL[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageView = null; // created as a global view
if (convertView == null) {
imageView = new ImageView(context);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(context).load(posterURL[position]).into(imageView);
return imageView;
}
}
OnCreateView OnCreate OnStart
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.fragment_main, container, false);
imageView = (ImageView) rootview.findViewById(R.id.list_itempostr);
grideview = (GridView) rootview.findViewById(R.id.movie_grid_view);
grideview.setAdapter(movieadapter);
movieadapter = new mcustomAdapter(getActivity(), imagearry);
movieadapter.notifyDataSetChanged();
grideview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
return rootview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updatemovies();
}
public void updatemovies() {
String sortby = "popularity.asc";
fetch.execute(sortby);
}
@Override
public void onStart() {
super.onStart();
//updatemovies();
}
FragmentMain
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityFragment"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/movie_grid_view"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
/>
</FrameLayout>
图片视图
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_itempostr"
/>
mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampel.android.mdb" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
尝试使用TMDB api,这样可以更轻松地获取数据。
将此添加到您的app/build.gradle
compile group: 'info.movito', name: 'themoviedbapi', version: '1.1'
然后像这样使用AsyncTask:
class fetchMovies extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
try {
String sortBy = params[0];
tmdbDiscover = new TmdbApi("YourApiKey").getDiscover();
if (tmdbDiscover != null) {
resultsPage = tmdbDiscover.getDiscover(new Discover().sortBy(sortBy).page(1));
if(resultsPage != null) {
moviesList = resultsPage.getResults();
}
}
} catch (Exception e) {
Log.i("Error:", e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(moviesList != null) {
movieArrayList = populateMoviesList(moviesList);
adapterMovies.setPosts(movieArrayList);
}
}
}
我想您正在Android Developer Nanodegree中制作项目2,因此here是相应的社区,您可以在其中找到有关项目和常见问题的许多不同问题/答案