Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Hector Romero
Hector Romero
6,151 Points

String post with okhttp

Hi!

I'm trying to post a string using okhttp as follows:

private void postCommand() {

    int channelId = 501;
    String api_key = "S6OT1D3MVEQ6J04D";
    String url = "https://api.thingspeak.com/talkbacks/" + channelId + "/commands";

    if (isNetworkAvailable()) {
        final MediaType text = MediaType.parse("text/x-markdown; charset=utf-8");

        OkHttpClient client = new OkHttpClient();

        String postBody = "api_key=" + api_key + "\n" + "command_string" + "TURN_ON";

        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(text, postBody))
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                alertUserAboutError();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String respuesta = response.body().string();
                    Log.v(TAG, respuesta);
                    if (response.isSuccessful()) {

                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception caught: ", e);
                }
            }
        });
    }
    else {
        alertUserAboutNetwork();
    }
}

But it doesn't work... It doesn't show errors when running but it seems to end up in onFailure.

Can you help me, please?

Thanks!

3 Answers

Hello,

I'm not familiar with thingspeak's API, however in

String postBody = "api_key=" + api_key + "\n" + "command_string" + "TURN_ON";

the newline character("\n") seems out of place. With the web, I believe those are usually encoded. I'd recommend at least double checking that part of your code, maybe running it through the debugger.

Hector Romero
Hector Romero
6,151 Points

James, thank you for your answer!

I've checked the part you pointed out and I've tried some configurations but they don't work:

String postBody = "api_key=" + api_key + "command_string" + "TURN_ON";
String postBody = "api_key=" + api_key + "&command_string" + "TURN_ON";

Thingspeak Talkback gives this example:

POST https://api.thingspeak.com/talkbacks/501/commands
     api_key=S6OT1D3MVEQ6J04D
     command_string=OPENDOOR

I don't get to make it work...

Thanks for any help.

One last thought I had is to change

String postBody = "api_key=" + api_key + "&command_string" + "TURN_ON";

to

String postBody = "api_key=" + api_key + "&command_string=" + "TURN_ON";

If that doesn't work, you're probably going to have to look more into encoding the strings better. I've been reading that Square has a library to help with that called Mimecraft, thought I have not used it. A few stackoverflow postings are available here and here for starters.