">mixi Developer Center (mDC)

mixi Connect

mixi Connect (English) » mixi Graph API » mixi API SDK for Android™ » Using APIs

Using APIs

Use the MixiContainer#send() method to access the Graph API. You can call all of the API in the same way.

This section describes how to use the send method to call an API. For details o

http://developer.mixi.co.jp/connect/mixi_graph_api

The following six types of methods are provided to explain how to use Graph API.

void send(String endpointPath, CallbackListener listener)

Use this for APIs that obtain information with the GET request. It is the simplest to use.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
CallbackListener listener Specify the callback method for receiving the results.

The following is a sample code that obtains the information of the People API user.

mContainer.send("/people/@me/@self", new CallbackListener() {});

void send(String endpointPath, Map, CallbackListener listener)

Use this when specifying the conditions with parameters and obtaining the information with the GET request.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
Map Specify the parameter in the Map type.
CallbackListener listener Specify the callback method for receiving the results.

The following is a sample code when specifying the conditions to access People API.

Map params = new HashMap();
params.put("fields", "thumbnailUrl profileUrl");
params.put("sortBy", "displayName");
params.put("sortOrder", "ascending");

mContainer.send("/people/@me/@friends", params, new CallbackListener() {});

void send(String endpointPath, HttpMethod method, CallbackListener listener)

Use this when specifying the HTTP method to access the API.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
HttpdMethod method Specify the HTTP method.
CallbackListener listener Specify the callback method for receiving the results.

The following is a sample code used when deleting the likes given to a "mixi voice."

mContainer.send("/voice/favorites/create/[Post-ID]", HttpMethod.DELETE, new CallbackListener() {});

void send(String endpointPath, HttpMethod method, Map, CallbackListener listener)

Use this when specifying the conditions with the parameters and making a request with any HTTP method.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
HttpdMethod method Specify the HTTP method.
Map Specify the parameter in the Map type.
CallbackListener listener Specify the callback method for receiving the results.

The following is a sample code that posts a tweet from a "mixi voice."

Map params = new HashMap();
params.put("status", "Voice test");

mContainer.send("/voice/statuses/update", HttpMethod.POST, params, new CallbackListener() {});

void send(String endpointPath, String contentType, InputStream stream, long length, CallbackListener listener)

Use this when you use the API that uploads binary data such as photos and images.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
String contentType Specify Content-Type (e.g., "image/jpeg").
InputStream stream Specify the data sent in POST Body.
long length Specify the size of the data sent in InputStream.
CallbackListener listener Specify the callback method for receiving the results.

The following is a sample code that posts a tweet with a photo onto "mixi voice."

InputStream is = FileInputStream("image.jpg");
String contentType = "image/jpeg";
String status = "foobar";

mContainer.send("/voice/statuses/update?status=" + status, contentType, is,
    is.available(), new CallbackListener(){});

void send(String endpointPath, JSONObject json, CallbackListener listener)

Use this when accessing the API that posts the JSON data.

Arguments

String endpointPath Specify the API end point (e.g., "/people/@me/@friends").
JSONObject json Specify JSONObject.
CallbackListener listener Specify the callback method for receiving the results.

Obtaining the results

In practice, the parameter's CallbackListener must perform the appropriate procedures as indicated below.

mContainer.send("/people/@me/@self", new Listener());

private class Listener implements CallbackListener {
    @Override
    public void onComplete(Bundle values) {
        // Normal processing
        String json = values.getString("response");
    }

    @Override
    public void onError(ErrorInfo e) {
        // Handling an error
        Log.e("mixi API Error", e.getErrorCode() + "," + e.getMessage());
    }

    @Override
    public void onCancel() {
        // Handling a user cancel
    }

    @Override
    public void onFatal(ErrorInfo e) {
        // Handling an abnormal termination
    }
}

When the request to the API is successfully completed, the results are passed on as the response parameter in the values argument of the onComplete method in the callback. For example, with the People API, you can obtain the following string in the JSON format by referencing it with values.getString("response").

{
  "entry" : [
    {
      "id" : "qgjw87yg3djw",
      "displayName" : "bert",
      "thumbnailUrl" : "http://img.mixi.net/img/basic/common/noimage_member180.gif",
      "profileUrl" : "http://mixi.jp/show_friend.pl?uid=qgjw87yg3djw"
    },
    ・・・
  ],
  ・・・
}

Handling errors

If the required scope is not authorized or the parameters are incorrect, an error occurs and the onError callback is called. You can obtain the details of error description from the ErrorInfo argument of this onError callback.

ErrorInfo#getErrorCode()

This obtains the HTTP status code of the API response. The following shows the details of the error codes.

Error code Overview
400 (BAD_REQUEST) Occurs when the parameters are incorrect.
403 (FORBIDDEN) Occurs when API access is not granted.
404 (NOT_FOUND) The API does not exist.
500 (INTERNAL_SERVER_ERROR) Internal error on the mixi side (please contact us when this error occurs).

ErorrInfo#getErrorMessage()

You can use getErrorMessage to obtain the details of the error in JSON format. The error message returned depends on the type of the API. For example, the following message is returned when the status parameter of Voice API is missing.

{"request":"/2/voice/statuses/update","error":"Invalid status."}

TOP OF THIS PAGE