使用Android的Quizlet API

时间:2013-07-06 10:16:42

标签: java android api mobile oauth-2.0

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

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

有谁知道我怎么能这样做?我已经多次尝试但未能这样做了:(甚至还没有接近甚至值得粘贴的东西。

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

1 个答案:

答案 0 :(得分:3)

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

}