Last active
April 16, 2016 22:18
-
-
Save johnwargo/9a4bf307287b5d808b089e5e2932c091 to your computer and use it in GitHub Desktop.
Android HTTP POST Request Using OKHTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.app_menu, menu); | |
//Only display the menu if the app is in Override mode | |
if (mIsOverride) { | |
//First add the click listener for the menu item | |
MenuItem mItem = menu.findItem(R.id.menu_item_one_time_code); | |
mItem.setOnMenuItemClickListener( | |
new MenuItem.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem item) { | |
//Create new single use code | |
UUID mCode = UUID.randomUUID(); | |
// Build a url the user can use to open the garage door | |
// we want to send it to someone so they can use it | |
// This URL works from ANYWHERE, so be careful passing it around. | |
// Also, it's a single-use code, so only works once. | |
final String openURL = Config.WEB_APP_URI + "?uuid=" + mCode.toString(); | |
//Send it to the Particle Cloud | |
try { | |
//Create the HTTP request body and populate it | |
RequestBody body = new FormBody.Builder() | |
.add("access_token", Config.ACCESS_TOKEN) | |
.add("params", mCode.toString()) | |
.build(); | |
//Call the Particle service | |
Request request = new Request.Builder() | |
.url(URL_SET_CODE) | |
.post(body) | |
.build(); | |
client.newCall(request).enqueue(new Callback() { | |
@Override | |
public void onResponse(Call call, Response response) throws IOException { | |
Headers responseHeaders = response.headers(); | |
for (int i = 0; i < responseHeaders.size(); i++) { | |
Log.i(TAG, "Header: " + responseHeaders.name(i) + ": " + responseHeaders.value(i)); | |
} | |
Log.i(TAG, "Response body: " + response.body().string()); | |
Log.i(TAG, "Message: " + response.message()); | |
if (response.isSuccessful()) { | |
SetCodeRunnable runnable = new SetCodeRunnable(openURL); | |
runOnUiThread(runnable); | |
} else { | |
throw new IOException("Unexpected code " + response); | |
} | |
} | |
@Override | |
public void onFailure(Call call, IOException e) { | |
Log.e(TAG, "Exception: " + e.getMessage()); | |
RunnableDialog runnable = new RunnableDialog(e.getMessage()); | |
runOnUiThread(runnable); | |
} | |
}); | |
} catch (Exception e) { | |
Log.e(TAG, "Exception: " + e.getMessage()); | |
RunnableDialog runnable = new RunnableDialog(e.getMessage()); | |
runOnUiThread(runnable); | |
} | |
//We're done here, so let the device OS know we dealt with the | |
//menu item | |
return true; | |
} | |
}); | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example is generating a single use code (for opening my garage door opener) and sending it to the Particle Cloud for delivery to the Particle Photon device.
Override mode is a boolean value I'm using to determine whether it's me and my wife doing this or my kids. If it's one of us, the menu appears. If not, nothing.