Shahek Shahek - 6 months ago 48
Android Question

stackoverflow error using gson

I am trying to get places of interest and enabled APIS are "google maps android API & google places API". When I post the url in web browser with my API_Key.

Error message:

" This IP, site or mobile application is not authorized to use this API key. Request received from IP address 212.241.70.124, with empty referer".

2nd thing when i run this code then i get error:
" java.lang.StackOverflowError: stack size 1036KB gson".
Kindly let me know what I am doing wrong? I would be really thankful to you!

Java Code:

public class FoodFragment extends Fragment {

private SupportMapFragment mapfragment;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.food_layout, null);

// I am getting Map here and it's okay.
googlemap();

PostFetcher postfetcher=new PostFetcher();
postfetcher.execute();

return view;
}



private class PostFetcher extends AsyncTask<Void,Void,String>{

private static final String TAG = "PostFetcher";
public static final String SERVER_URL = " https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=50&type=restaurant&name=cruise&key=My APi key here";


@Override
protected String doInBackground(Void... params) {

try {

Gson gson = new GsonBuilder().create();
FoodFragment jsondata = gson.fromJson(SERVER_URL, FoodFragment.class);

System.out.println("data="+jsondata);

} catch (Exception ex) {
Log.e(TAG, "Failed to parse JSON due to: " + ex);
}
return null;
}


}

}

Answer
FoodFragment jsondata = gson.fromJson(SERVER_URL, FoodFragment.class);

FoodFragment.class is the name of your current class file, not a Java object that the JSON represents. That second parameter must be a POJO and the first, a JSON string, not a URL.

It would seem you need to make a HTTP request first, then you may do

MapData map = gson.fromJson(serverResponse, MapData.class);

After you create a MapData class, of course.