I am trying to understand how to pass the "uuid" value found in the response message from an HTTP Post to a subsequent point in my code. The postRequest works properly (my testing uses a textbox named "item_success_id" where I can see the newly created "uuid", but then I need to use that "uuid" in building a new endpoint for my okHttpHandler to upload an image to associate with the "uuid".
The problem is that the run() method can't return anything so I think I need to use Callable instead, but I can't figure out how to use it properly.
Also, there is a constraint that myResponse cannot be invoked twice. The API expects the client to capture the response and cache it itself if needed. That's what I need to be able to do. I don't think it should be a huge change to move from a runnable to a callable approach, but it seems to be. Thanks for any help you can provide.
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_create_item_image:
try {
postRequest(createItemEndpoint, createItemBody);
OkHttpHandler okHttpHandler = new OkHttpHandler();
okHttpHandler.execute(createImageEndpoint);
} catch (IOException e) {
e.printStackTrace();
}
break;
void postRequest (String postUrl, String postBody) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, postBody);
Request request = new Request.Builder()
.url(postUrl)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
PicActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
item_success_id.setText("Item UUID: " + json.getString("uuid"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
Thanks for the help guys, but I wasn't able to figure out exactly how to use your input. I couldn't get the interface implemented correctly. So, since I realized that I don't actually need to do anything with the variable in the activity I eliminated the runonuithread code, and then I also realized I could just use the uuid right where I was getting it and so just moved my entire OkHttpHandler code to inside my postRequest method so it would have immediate access to it. No need to save it and keep it for anything.