23tony
Well-Known Member
I have an Async private inner class that I'm implementing on every Activity, that gets HTML from a URL. With the exception of the URL, it's the same in every Activity.
I'm wondering how to go about abstracting it so that i can just pass in the URL and get the response back. I'm having two problems with this: 1) I need a context to pass to the requestQueue, and 2) I need a way to get the data back.
I have tried implementing the code a standalone Async class but I keep getting errors trying to pass the context (says the context can't be applied to HTMLLoader). The next thought I had was to implement a regular class, then to use an Async class in the Activity, get the context from there, and pass that in - but I get the same error, just in the activity's async class instead.
I'm going to have a few activities needing to access various URLs so I don't want to keep repeating the same private inner class over & over. It might work, but it's not a clean solution. It's probably something about Java or Android that I don't know, but unfortunately, I don't know what it is I don't know... Any suggestons appreciated.
Here's the inner class code:
I'm wondering how to go about abstracting it so that i can just pass in the URL and get the response back. I'm having two problems with this: 1) I need a context to pass to the requestQueue, and 2) I need a way to get the data back.
I have tried implementing the code a standalone Async class but I keep getting errors trying to pass the context (says the context can't be applied to HTMLLoader). The next thought I had was to implement a regular class, then to use an Async class in the Activity, get the context from there, and pass that in - but I get the same error, just in the activity's async class instead.
I'm going to have a few activities needing to access various URLs so I don't want to keep repeating the same private inner class over & over. It might work, but it's not a clean solution. It's probably something about Java or Android that I don't know, but unfortunately, I don't know what it is I don't know... Any suggestons appreciated.
Here's the inner class code:
Code:
private class HTMLLoader extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
}
catch (java.lang.InterruptedException interruptedException) {
}
RequestQueue queue = Volley.newRequestQueue(SecondActivity.this);
String url = "http://10.0.2.2";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextBox.setText(response);
mProgressBar.setVisibility(View.INVISIBLE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String err = error.toString();
mTextBox.setText(err);
mProgressBar.setVisibility(View.INVISIBLE);
}
});
queue.add(stringRequest);
return null;
}
}