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 trialJorge Flores
7,864 PointsCan't get correct response from API
Hi, I'm trying to do this app but I'm stock because I dont receive a json array as a response. I am using the location that it's being used in the videos, which is also the example that is used in the documentation of the forecast API. Instead I'm receiving the following string: "com.squareup.okhttp.internal.http.RealResponseBody@d0c4f9b". Does anybody know why?
3 Answers
Jorge Flores
7,864 PointsThis is the code, it's basicly the one made in the videos. The part where I'm receiving the string instead of the JSON array it's on the on response method of the callback function.
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private CurrentWeather mCurrentWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String apiKey ="731df217cbd579c1bed946fd0168a114";
double latitude=37.8267;
double longitude=-122.423;
String forecastURL="http://api.forecast.io/forecast/731df217cbd579c1bed946fd0168a114/37.8267,-122.423";
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(forecastURL).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.i(TAG, "on failure");
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().toString();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
} else {
alertUserAboutError();
}
} catch (JSONException e){
Log.e(TAG, "Exception caught", e);
}
}
});
} else {
Toast.makeText(this, R.string.network_unavailable_message, Toast.LENGTH_LONG).show();
}
Log.d(TAG, "Main UI code is running");
}
private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
Log.i(TAG, "From JSON "+timezone);
JSONObject currently = forecast.getJSONObject("currently");
CurrentWeather currentWeather= new CurrentWeather();
currentWeather.setmHuidity(currently.getDouble("humidity"));
currentWeather.setmTime(currently.getLong("time"));
currentWeather.setmIcon(currently.getString("icon"));
currentWeather.setmPrecipChance(currently.getDouble("precipProbability"));
currentWeather.setmSummary(currently.getString("summary"));
currentWeather.setmTemperature(currently.getDouble("temperature"));
return currentWeather;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo!=null && networkInfo.isConnected()){
isAvailable=true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
}
Jorge Flores
7,864 PointsI should be getting the following json array
Jorge Flores
7,864 PointsI found my mistake in the following line of code:
String jsonData = response.body().toString();
Should be this:
String jsonData = response.body().string();
Sagar Suri
6,043 PointsYeah..you are right.:-)
Sagar Suri
6,043 PointsSagar Suri
6,043 Pointspaste the code where you have used JSONArray to create an array object of type JSONArray