Search code examples
androidgoogle-glassgoogle-mirror-api

Correct way to generate an httprequest to mirror api service from android


i try to push a timeline card to the Glass from my android application. i use this code:

String BASE_URL = "https://www.googleapis.com/upload/mirror/v1/";

    try {
        final HttpPost request = new HttpPost();
        request.setURI(new URI(BASE_URL + "timeline"));
        request.addHeader("Authorization", String.format("Bearer %s", token));
        request.addHeader("Content-Type", "application/json");
        request.setEntity(new StringEntity(json.toString()));

        // Execute the request on a background thread
        mThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    final HttpResponse response = mClient.execute(request);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.onSuccess(response);
                            }
                        });
                    } else {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.onFailure(response, null);
                            }
                        });
                    }
                } catch (final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onFailure(null, e);
                        }
                    });
                }
            }
        });
    } catch (UnsupportedEncodingException e) {
        // Note: This should never happen
    } catch (URISyntaxException e) {
        // Note: This should never happen
    }

the json is like this :

{"notification":{"level":"DEFAULT"},"text":"Pizza and spaghetti"}

but the service respond with an error :

"error": {
"errors": [
{
"domain": "global",
"reason": "badContent",
"message": "Media type 'application/json' is not supported. Valid media types: [image/*, audio/*, video/*]"
}
],
"code": 400,
"message": "Media type 'application/json' is not supported. Valid media types: [image/*, audio/*, video/*]"
}
}

i follow the code from here: https://github.com/twaddington/mirror-quickstart-android

any idea?

Ps. there is another way to push a timeline card from an android application to the google glass?

Thanks


Solution

  • As you can see in the Google Docs (https://developers.google.com/glass/v1/reference/timeline/insert) this request only accept mutimedia data as 'Content-type'.

    Try changing this line with the correct content type (image/, audio/, video/*) : request.addHeader("Content-Type", "application/json");

    You will have to add the multimedia object inside your HTTPost object. More info about that: Sending images using Http Post

    --EDIT-- The request you want to do (only metadata, without multimedia) has to be done to: https://www.googleapis.com/mirror/v1/ (notice that there is not the upload word).