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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Hector Romero](https://secure.gravatar.com/avatar/be9bb6431e31974ae809301cce61ba70?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Hector Romero
6,151 PointsString 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
![James Simshaw](https://secure.gravatar.com/avatar/f01e346c98a43044a15179643be83cb3?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
James Simshaw
28,738 PointsHello,
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](https://secure.gravatar.com/avatar/be9bb6431e31974ae809301cce61ba70?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Hector Romero
6,151 PointsJames, 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.
![James Simshaw](https://secure.gravatar.com/avatar/f01e346c98a43044a15179643be83cb3?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
James Simshaw
28,738 PointsOne 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.