2

我正在尝试学习如何使用适用于 Android 的 Quizlet API,我已经阅读了在线文档,但我仍然完全迷失了方向。

首先,我使用的是 Eclipse Juno 开发环境。我希望应用程序做的就是让用户在搜索框中输入搜索词,从网站上找到所需的卡片,然后根据需要将它们安装到他们的设备上。我还希望他们访问他们的帐户并查看他们创建的套牌。API 网站在这里:https ://quizlet.com/api/2.0/docs/

有谁知道我该怎么做?我已经尝试了很多次,但都失败了:(甚至还没有接近值得粘贴的东西。

任何帮助都会很棒,感谢阅读。

4

1 回答 1

2

这里有一些可能会派上用场的 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 ;
}

}
于 2013-07-06T22:04:07.217 回答