public class getImagesTask extends AsyncTask<Void, Void, Void>
{
JSONObject json;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(GoogleSearchAPIExampleActivity.this, "", "Please wait...");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
URL url;
try {
url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q="+strSearch+"&rsz=8&start=0" );
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", "http://technotalkative.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println("Builder string => "+builder.toString());
json = new JSONObject(builder.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(dialog.isShowing())
{
dialog.dismiss();
}
try
{
JSONObject responseObject = json.getJSONObject("responseData");
JSONArray resultArray = responseObject.getJSONArray("results");
listImages = getImageList(resultArray);
Toast.makeText(getBaseContext(), "Length :" + listImages.size(), 5000).show();
// SetListViewAdapter(listImages);
System.out.println("Result array length => "+resultArray.length());
Intent in = new Intent(GoogleSearchAPIExampleActivity.this, Second.class);
startActivity(in);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上記は、google image api を使用して画像を取得するコードです。しかし、ここでは 1 回の検索で 8 枚の画像しか取得できません。8枚以上の画像を取得したい場合はどうすればよいですか?