Reputation: 1127
My app makes a call to FourSquare API. The call happens in my getResponse()
function, and that function gets called every time I do new Explore().execute();
Now, I am able to get a string from the API... But when I pass that string to displayResults()
function, it becomes null (I have a code comment below to show exactly where). And because this string becomes null, I can not parse the JSON. What is causing this issue?
public TextView mBusinessName;
public TextView mCategorie;
public WebView mLocationView;
private class Explore extends AsyncTask<Void, String, String>{
String resp = "";
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... String) {
//I get a complete JSON string and assign to resp HERE.
resp = getResponse();
return null;
}
@Override
protected void onPostExecute(String s) {
//pass resp to display results
displayResults(resp);
}
}
public String getResponse(){
String clientSecret = getResources().getString(R.string.client_secret);
String clientID = getResources().getString(R.string.client_id);
String url = "https://api.foursquare.com/v2/venues/explore?ll="
+ mLatitude + "," + mLongitude
+ "&limit=" + 5
+ "&radius=" + mRadius
+ "&query=" + mTerm
+ "&oauth_token="
+ "&client_secret="+ clientSecret
+ "&client_id="+ clientID
+ "&v=20150610";
String getResponseString = "";
try{
URL searchUrl = new URL(url);
HttpURLConnection httpsClient =
(HttpURLConnection) searchUrl.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpsClient.getInputStream()));
try{
getResponseString = "";
while((getResponseString = reader.readLine()) != null){
Log.d("Response ==== ", getResponseString);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return getResponseString;
}
public void displayResults(String resp){
//HERE is where it fails. The String resp becomes null/empty
// have tried logging resp, the log doesn't show up at all
// because of this, JSON string can not be parsed!!!!
try {
JSONObject json = new JSONObject(resp);
JSONArray items = json.getJSONObject("response")
.getJSONArray("groups").getJSONObject(0)
.getJSONArray("items");
//randomize items
JSONObject item = items.getJSONObject(getRandomIndex(items.length()-1));
String name = item.getJSONObject("venue").optString("name");
String categorie = item.getJSONObject("venue").getJSONArray("categories").getJSONObject(0).getString("name");
String latitude = item.getJSONObject("venue").getJSONObject("location").optString("lat");
String longitude = item.getJSONObject("venue").getJSONObject("location").optString("lng");
String image = "http://maps.google.com/maps/api/staticmap?center="
+ latitude + "," + longitude
+ "&markers=size:tiny%color:red%7C" + latitude + "," + longitude
+"&zoom=17&size=375x225&sensor=false";
mLocationView.loadUrl(image);
mBusinessName.setText(name);
mCategorie.setText(categorie);
} catch (JSONException e) {
e.printStackTrace();
}
}
EDIT: The variable resp
becomes null/empty inside the function displayResults()
. I don't know how this is happening.
Upvotes: 0
Views: 44
Reputation: 10687
Your getResponse()
method does nothing. You need to change it so that it actually returns the complete string.
try{
StringBuilder sb = new StringBuilder();
getResponseString = "";
while((getResponseString = reader.readLine()) != null){
Log.d("Response ==== ", getResponseString);
sb.append(getResponseString);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return sb.toString();
Upvotes: 1