I have a ImageAapter class which is used to update grid view.
I want to upload images from url - "http://image.tmdb.org/t/p/w185/" + "poster path for different movies" which I a getting from the movie database api.
this is my get poster path code ---
public String[] getposterpath(String movieJsonStr)
throws JSONException{
JSONObject movieJson = new JSONObject(movieJsonStr);
JSONArray movieArray = movieJson.getJSONArray("results");
String[] resultStrs = new String[movieArray.length()];
String[] posterpath = null;
for(int i = 0; i < movieArray.length(); i++){
JSONObject dayForecast = movieArray.getJSONObject(i);
posterpath = dayForecast.getString("poster_path");
}
return posterpath;
}
and I am executing this method in async task --
public class fetchposter extends AsyncTask {
private final String LOG_TAG = fetchposter.class.getSimpleName();
// getposterpathfunction

@Override
protected String[] doInBackground(String... params) {
if (params.length == 0) {
return null;
}

// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String movieJsonStr = null;

try {
//final String
URL pos_BASE_URL =
new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=8d7a48043ba1d3348181e2b6615cedc7");

urlConnection = (HttpURLConnection) pos_BASE_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) {
// 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.
return null;
}
movieJsonStr = buffer.toString();
Log.v(LOG_TAG, "Forecast JSON STRING: " + movieJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
} 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 getposterpath(movieJsonStr);
// return movieJsonStr;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}



}


this is my image adapter code --
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private String movieJsonStr = null;
private String[] pos_path;
private String[] pos_path_str;
public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {

return 5;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's 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;
}
try{
URL pos_BASE_URL =
new URL("http://image.tmdb.org/t/p/w185/");
// movieJsonStr = new fetchposter().doInBackground();
// String[] pos_path = getposterpath(movieJsonStr) ;
String[] pos_path = new fetchposter().doInBackground();
pos_path_str[position] = pos_path[position].toString(); -- getting a null pointer exception
URL final_url = new URL(pos_BASE_URL,pos_path_str[position]);
String url_str = final_url.toString();

Picasso.with(mContext).load(url_str).into(imageView);

}
catch(IOException e){
Log.e("LOG_TAG", "Error ", e);

}
//catch (JSONException e){
// Log.e("LOG_TAG", e.getMessage(), e);
// e.printStackTrace();
// }


return imageView;