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获取图片的代码。但在这里,我只获得了8张图片,仅供1次搜索。如果我想获得超过8张图像需要做什么?
答案 0 :(得分:0)
继续我上面的评论,文档说明
rsz rsz=4 This argument supplies an integer from 1–8 indicating the number of results to return per page.
所以看起来你必须自己通过新的电话来浏览结果
了解cursor
here