Reputation: 5845
Currently in my project, I am making Http requests and I want different http response to be sent to different callback methods.
I wrote a quick sample below to show what I want to do. I know it probably wont be possible the way i want it, but are there any clean solutions to achieve the same thing?
Sample:
Activity Class:
public class Main extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Services service = new Services();
service.login("user", "password", **onLoginComplete()** );
}
public void onLoginComplete(String HTTPResponse){
// Do something with the response
}
}
Service Class:
public class Services{
public void login(String user, String password, CALLBACK){
Request request = createLoginRequest(user, password);
sendRequest(request, CALLBACK);
}
public class sendRequest extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// Do Http Request
// Get Response
CALLBACK(response);
}
}
}
Upvotes: 8
Views: 27712
Reputation: 51
I think I had the same problem as yours.
I was looking for a good answer and this was the implementation that worked for me:
First create an interface which contains your methods; in my case I use the typical onSuccess
and onFailure
but you can make your own methods:
//MyInterface.java
public interface MyInterface
{
void onSuccess(String response);
void onFailure(String response);
}
Then create class Services
:
//Services.java public class Services { public void login(String user, String password, MyInterface myInterface) { Request request = createLoginRequest(user, password); if(/*Request successful*/) myInterface.onSuccess("Login succesful"); else myInterface.onFailure("Login failed"); } }
And finally call the method on your Activity
:
//Main.java public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Services service = new Services(); service.login("user", "password", new Myinterface() { @Override public void onSuccess(String response) { Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(String response) { Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show(); } }); } }
Upvotes: 3
Reputation: 21
If I understand you correctly, what you are trying to achieve is recommended to be done via the use of an AsyncTask. It's explained in a very simple way here http://developer.android.com/reference/android/os/AsyncTask.html
Additionally, I share an example of how I execute (doInBackground) GET requests to a site and which result then I read (onPostExecute)... hope it helps!
protected InputStream doInBackground(String... example) {
JsonComm jc = new JsonComm();
String baseUrl = "http://www.somewhere.com/get_request.php?data=";
String jcString = jc.encodeJSON("nowyou","seeme");
String url = "";
try {
url = baseUrl + URLEncoder.encode(jcString, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse response;
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
response = httpClient.execute(request);
} catch (Exception ex) {
// handle exception here
}finally {
httpClient.getConnectionManager().shutdown();
}
return response.getEntity().getContent();
}
protected void onPostExecute(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(sb.toString());
}
Upvotes: 2
Reputation: 7585
How to implement callbacks in java:
public interface SomeCallbackInterface {
public void finished(Request req);
}
Then in your class you do:
YourReqeust.instantiateWithCallback(new SomeCallbackInterface() {
@Override
public void finished(Request req){
// do something here
}
});
This pretty much the same thing your do with any View.OnClickListener
Upvotes: 0
Reputation: 36302
There are several ways you can handle this. You could pass in a Runnable
for your callback or you could provide a method to override in your Services
class and use an anonymous class derived from Services
in your main activity. If you need to pass in a parameter, you could also define an interface for something equivalent to the Runnable
where you can define a method with a response parameter.
Upvotes: 0
Reputation: 2976
interface OnLoginCompleteListener {
void onLoginComplete(String response);
}
And then
public void login(String user, String password, OnLoginComplete listener) {
mOnCompleteListener = listener;
}
and
protected Object doInBackground(Object... params) {
mOnCompleteListener.onLoginComplete(response);
}
and finally
service.login("user", "password", new OnLoginCompleteListener() {
public void onLoginComplete(String response) {
// Handle your response
}
});
Upvotes: 21