这里有一些可能会派上用场的 android/java 代码。
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.util.Log;
import com.google.gson.stream.JsonReader;
public class QuizletCatalog {
class RowData {
int id;
String name;
String description;
int numCards ;
String lastModified ;
}
// edit the line below with your quizlet client id
private static final String QUIZLET_CLIENT_ID = "_PUT_YOUR_CLIENT_ID_HERE_";
private static final String browseApiUrl = "https://api.quizlet.com/2.0/search/sets?client_id=" + QUIZLET_CLIENT_ID + "&time_format=fuzzy_date" ;
private static final String getSetApiUrl = "https://api.quizlet.com/2.0/sets?client_id=" + QUIZLET_CLIENT_ID + "&set_ids=" ;
private String username;
private String searchPhrase;
private int page = 1; // 1 based page number
private int totalPages ;
private String errorDescription ;
private String errorTitle ;
public QuizletCatalog(final Context appContext, final String username, final String searchPhrase) {
this.username = username;
this.searchPhrase = searchPhrase ;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSearchPhrase() {
return searchPhrase ;
}
public void setSearchPhrase( String searchPhrase ) {
this.searchPhrase = searchPhrase ;
}
public List<RowData> firstPage() throws IOException {
page=1;
return openPage();
}
public List<RowData> nextPage() throws IOException {
page++;
return openPage();
}
private List<RowData> openPage() throws IOException {
this.errorDescription = null ;
this.errorTitle = null ;
List<RowData> list = new ArrayList<RowData>();
InputStream inputStream = null ;
try {
URL url = new URL( getCatalogUrl() );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if ( connection.getResponseCode() >= 400 ) {
inputStream = connection.getErrorStream();
}
else {
inputStream = connection.getInputStream();
}
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
reader.beginObject();
while ( reader.hasNext() ) {
String name = reader.nextName();
if ( "total_pages".equals( name )) {
this.totalPages = reader.nextInt();
if ( page > totalPages ) {
}
}
else if ( "page".equals( name )) {
this.page = reader.nextInt();
}
else if ( "error_title".equals( name )) {
errorTitle = reader.nextString();
}
else if ( "error_description".equals( name )) {
errorDescription = reader.nextString();
}
else if ( "sets".equals( name ) ) {
reader.beginArray();
while ( reader.hasNext() ) {
list.add( parseSetJson( reader ) );
}
reader.endArray();
}
else {
reader.skipValue();
}
}
reader.endObject();
}
finally {
if ( inputStream != null ) {
inputStream.close();
}
}
return list ;
}
RowData parseSetJson( JsonReader reader ) throws IOException {
reader.beginObject();
RowData rowData = new RowData();
while ( reader.hasNext() ) {
String name = reader.nextName();
if ( name.equals( "title" )) {
rowData.name = reader.nextString();
}
else if ( name.equals( "id" )) {
rowData.id = reader.nextInt();
}
else if ( name.equals( "term_count" )) {
rowData.numCards = reader.nextInt();
}
else if ( name.equals( "modified_date" )) {
long value = reader.nextLong();
rowData.lastModified = Data.SHORT_DATE_FORMAT.format( new Date( value * 1000 ) );
Log.d( Data.APP_ID, " modified_date value=" + value + " formatted=" + rowData.lastModified + " now=" + (new Date().getTime()) );
}
else {
reader.skipValue();
}
}
reader.endObject();
return rowData ;
}
public List<RowData> prevPage() throws IOException {
if ( page > 1 ) {
page--;
}
return openPage();
}
public String getErrorDescription() {
return this.errorDescription ;
}
public String getErrorTitle() {
return this.errorTitle ;
}
public static String getDeckUrl(String id) {
return getSetApiUrl + id;
}
public String getCatalogUrl() {
StringBuilder sb = new StringBuilder();
sb.append( QuizletCatalog.browseApiUrl );
sb.append( "&q=" ) ;
if ( this.username != null && this.username.length() > 0 ) {
sb.append( "creator:" + username + " " );
}
sb.append( this.searchPhrase );
sb.append( "&page=" );
sb.append( page );
Log.d( Data.APP_ID, sb.toString() );
return sb.toString() ;
}
public int getPage() {
return this.page ;
}
public int getTotalPages() {
return this.totalPages ;
}
}