java - 使用适用于 Android 的 Quizlet API

标签 java android api mobile oauth-2.0

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

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

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

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

最佳答案

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

}

关于java - 使用适用于 Android 的 Quizlet API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17501933/

相关文章:

java - 如何在 Spring Integration 中路由处理过程中抛出异常的消息?

java - 如何在java中使用jsoup用字符替换html标签

android - Colorfy 应用程序开发人员如何在包含许多图像的情况下保持应用程序的小尺寸?

java - 执行配置更改时出错 org.wso2.config.mapper.ConfigParserException : Template directory

java - Scala:将 org.w3c.dom.Document 转换为 scala.xml.NodeSeq

android - java.lang.RuntimeException : Unable to resume activity Android 错误

android - 如何处理listview android中的复选框?

java - Google map api 不显示我当前的位置

api - 如何将凭据传递给 Sonar API 调用?

java - Netbeans GUI 生成器 : how to edit generated code