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
ian sobo
3,851 PointsHTTP POST Response for Requestbin (Extra Credit)
Hey guys, I'm trying to do the Extra Credit section at the end of the "Networking" section in Make a Weather App. I can do a GET Request, by doing what Ben says to do in the next video, but i cannot figure out how to do a POST Request in my Android app here, and i really cannot figure it out by following the POST a string recipe in the wiki for OkHttp. Here's what i have so far:
package com.example.ian.treehouseweatherapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
public class MainActivity extends ActionBarActivity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String apiKey = "3b2a6c9aed11e80da43168ee303b588c";
double latitude = 37.8267;
double longitude = -122.423;
String forecastURL = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
String requestBinURL = "http://requestb.in/1cr03dh1";
OkHttpClient client = new OkHttpClient();
String dataToPost = "This is the string that will be posted!";
RequestBody requestBody = RequestBody.create(mediaType, dataToPost);
Request request = new Request.Builder().url(requestBinURL).post(requestBody).build();
}
}
1 Answer
ian sobo
3,851 PointsAlright ! I sorta did it! I went ahead and did the asynchronous thing, with just a .post inside of the Request.Builder chain, and requestbin detects that it was uploaded! However, the actual URL that i follow still has the default "ok" plaintext when i go to it. I thought POSTing data was a way to change the stuff that was displayed on the site, because i just POSTed a string? On the other hand, i know that whatever is displayed is determined by the site's HTML file, so in order for that to happen, would there need to be backend server code that would update the HTML to reflect whatever was sent in the HTTP Post Request, am i correct in assuming this? Also, here is my code for what worked:
package com.example.ian.extracredithttppostrequest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
String requestBinURL = "http://requestb.in/146e22o1";
String dataToPost = "This is the string that will be posted!";
RequestBody requestBody = RequestBody.create(mediaType, dataToPost);
Request request = new Request.Builder()
.url(requestBinURL)
.post(requestBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback(){
@Override
public void onFailure(Request request, IOException e) {
Log.e(TAG, "(onFailure) The request was not successful");
}
@Override
public void onResponse(Response response) throws IOException {
try {
Log.v(TAG, response.body().string());
if (response.isSuccessful()) {
Log.v(TAG, "Was successful!");
} else {
Log.v(TAG, "(onResponse) Was not successful");
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
Log.d(TAG, "Main UI code is running");
}
}