i'm not sure this is the perfect title for this but its the best i could come up with.
I have a java class ApiRequest the runs some http requests and returns the result in a callback via an interface. Example will be the authenticate method below:
public class ApiRequest {
private Context context;
private ApiRequestCallback api_request_callback;
public ApiRequest ( Context context ){
this.context = context;
// this can either be activity or context. Neither works in fragment
// but does in activity
this.api_request_callback = ( ApiRequestCallback ) context;
}
public interface ApiRequestCallback {
void onResponse(JSONObject response );
void onErrorResponse(JSONObject response );
}
public JsonObject authenticate(){
.... do stuff and when you get response from the server. This is some
kinda of async task usually takes a while
// after you get the response from the server send it to the callback
api_request_callback.onResponse( response );
}
Now i have a fragment class in a tablayout, that implements this class below
public class Home extends Fragment implements ApiRequest.ApiRequestCallback
{
// I have tried
@Override
public void onViewCreated(.........) {
api_request = new ApiRequest( getContext() );
}
// and this two
@Override
public void onAttach(Context context) {
super.onAttach(context);
api_request = new ApiRequest( context );
}
@Override
public void onResponse(JSONObject response) {
//I expect a response here
}
}
The response i get is that i can cannot cast: the activity context to the interface.
Java.lang.ClassCastException: com.*****.**** cannot be cast to com.*****.****ApiRequest$ApiRequestCallback
But this works with a regular activity so its really has me on the edge. A fix for this will be greatly appreciated. A teachable moment for me i'd say. Thanks