我正在做书籍查找器Android应用程序,它从电子书网站获取数据,每页显示10本书,当我按下一个按钮时,它应该显示下10本书。
以上网址在第一页上为我提供了一系列json书籍,它看起来像这样:
{"错误":" 0""时间":0.0043,"总计":" 327" ," Page":1," Books":[{" ID":1542146786," Title":" Java Phrasebook&# 34;,"描述":" Java短语手册为您提供代码短语........................... .... 等等
此网址 - http://it-ebooks-api.info/v1/search/java&type=title & page = 5
应该在第5页
上给我下一小块json数据我确实构建了有效的网址,但是使用GET请求方法读取的httpUrlconnection方法总是只给我第一页 - 即使我为第5页创建了网址。
这是建立URL连接并从给定URL存储到缓冲区中的方法:
private String MakeConnectionAndStoreBufferData(String validUrl){
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
URL url = new URL(validUrl);
// even if this url has &page=5 at the end
// it still fetches data for page = 0
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
return buffer.toString();
编辑:在Rami,我在这里展示了我如何构建我的URL地址 - 我在构建它们之后检查它们,它们看起来像预期的那样:
public class ProcessData extends AsyncTask<String, Void, String> {
private String createValidURL(String query, int page_counter) {
Log.d(LOG_TAG, "create Valid url......");
String BASE_URL = "http://it-ebooks-api.info/v1/search";
String resultUrl = null ;
if (page_counter == 0){
destinationURL = Uri.parse(BASE_URL).buildUpon()
.appendPath(query)
.appendQueryParameter("type", "title")
.build();
resultUrl = destinationURL.toString();
}
else if(page_counter > 0){
destinationURL = Uri.parse(BASE_URL).buildUpon()
.appendPath(query)
.appendQueryParameter( "type", "title")
.appendQueryParameter("page", Integer.toString(page_counter) )
.build();
resultUrl = destinationURL.toString();
}
Log.d(LOG_TAG, "before we return url, it is :" + resultUrl);
return resultUrl ;
}
public String doInBackground(String ...params) {
String validUrl = createValidURL(params[0], 5);//5 is page counter
String result_buffer = MakeConnectionAndStoreBufferData(validUrl);
return result_buffer ;
}
}
这是mainactivity.java,我搜索标题为&#34; java&#34;的书籍:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create valid URL, connect to it-ebooks website, read buffer, parse buffer to return complete list of books
ProcessData data = new ProcessData();
data.execute("java");//this calls doInBackground in processData class!
}
答案 0 :(得分:1)
您的MainActivity.java
public class MainActivity extends ActionBarActivity {
Button btnPrev, btnNext;
static int page = 1;
TextView txtview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPrev = (Button) findViewById(R.id.previous);
btnNext = (Button) findViewById(R.id.next);
txtview = (TextView) findViewById(R.id.txtview);
callServer(page);
btnPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (page > 1) {
page = page - 1;
callServer(page);
}
}
});
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
page = page + 1;
callServer(page);
}
});
}
protected void callServer(int pageNumber) {
new ParserClass(MainActivity.this, new CalbackListener() {
@Override
public void onSuccess(JSONObject result) {
try {
String pageNo = result.getString("Page");
txtview.setText("Page No : " + pageNo);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, page).execute();
}
}
activity_main.xml
<RelativeLayout 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="com.example.soapsample.MainActivity" >
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Previous" />
<Button
android:id="@+id/next"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="@+id/previous"
android:text="Next" />
</LinearLayout>
</RelativeLayout>
JsonParser.java类
public class ParserClass extends AsyncTask<Void, Void, JSONObject> {
private ProgressDialog dialog;
public interface CalbackListener {
public void onSuccess(JSONObject result);
}
private int page;
private CalbackListener callback;
private String url = "http://it-ebooks-api.info/v1/search/java&type=title&page=";
private Context context;
public ParserClass(Context context, CalbackListener callback1, int page) {
this.callback = callback1;
this.page = page;
this.context = context;
this.url += String.valueOf(this.page);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(context, "Title", "Loading...", true);
}
@Override
protected JSONObject doInBackground(Void... params) {
String result = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
Log.i("url", url);
HttpResponse httpResponse = null;
if (httpGet != null) {
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
Log.i("Result", result);
}
return new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
dialog.dismiss();
callback.onSuccess(result);
}
}
答案 1 :(得分:0)
http://it-ebooks-api.info/v1/search/java?type=title&page=5
经过仔细观察,我发现了错误 - 这是关于?在单词java之后签名。
基本上你可以写.... java?type = title&amp; pa它仍会加载第一页!该网站是“宽容的”#34;默认情况下将返回第一页如果查询格式错误 - 如果页面缺失,不完整等
我找不到其他方式!唯一的问题是java? -question标志, 无论如何,没有人可以帮助我,我很高兴我自己找到了这个虫子!
我应该确保我的网址已被替换?与&amp;。
P.S。小心创建URL:追加查询参数可能会使URL几乎正确。