I implemented Volley singleton (there are few examples over the internet) in order to use it from different activities.
For some reason, requests are not being sent to server, and when I inspect it on debug mode it looks that there are waiting in the volley request queue.
This is my singleton:
public class VolleyInternetOperator
{
private static VolleyInternetOperator mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private VolleyInternetOperator(Context context)
{
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleyInternetOperator getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleyInternetOperator(context);
}
return mInstance;
}
public static synchronized VolleyInternetOperator getInstance()
{
if (null == mInstance)
{
throw new IllegalStateException(VolleyInternetOperator.class.getSimpleName() +
" is not initialized, call getInstance(...) first");
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
public interface responseFunctionInterface
{
void apply(JSONObject response, JSONObject params);
}
public interface errorFunctionInterface
{
void apply(VolleyError error, JSONObject params);
}
public void accessWebsiteWithVolley(String url, int requestMethod,
final JSONObject params, final HashMap<String, String> additionalHeaders,
final responseFunctionInterface responseFunction, final errorFunctionInterface errorFunction)
{
CustomJSONObjectRequest jsonRequest =
new CustomJSONObjectRequest(requestMethod, url, params,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("got", "response");
responseFunction.apply(response, params);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
errorFunction.apply(error, params);
}
})
{
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
if (null != additionalHeaders) {
Iterator it = additionalHeaders.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry<String, String> pair = (Map.Entry<String, String>) it.next();
headers.put(pair.getKey(), pair.getValue());
}
}
return headers;
}
};
addToRequestQueue(jsonRequest);
}
}
I initialized it in the main activity onCreate:
VolleyInternetOperator.getInstance(this.getApplicationContext());
In order to send jsonRequest I'm using the following code:
VolleyInternetOperator.getInstance().accessWebsiteWithVolley(url, requestMethod, null, headers,responseFunction, errorFunction);
The request works when I copy the relevant parts to my activity, so I guess it's related to the singleton.
Thanks!
Edit 1:
Apparently it works if I'm calling the singleton from within my main activity, but if I create an object (I have dataCollector object) that calls it, it doesn't work.
Related
I am trying to use Volley to send 3 strings to a php script that sends it to a localhost server. I have this so far;
RegisterRequest;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
private Map<String, String> params;
public RegisterRequest(String username, String password,String isAdmin,
Response.Listener<String> listener,
Response.ErrorListener errListener){
super(Method.POST, REGISTER_REQUEST_URL,listener,errListener);
params = new HashMap<>();
params.put("username",username);
params.put("password",password);
params.put("isAdmin",isAdmin+"");
}
public Map<String, String> getparams() {
return params;
}
}
This is CreateUser;
public class CreateUser extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_user);
this.setTitle("Create User");
final EditText username1 = findViewById(R.id.Createusername);
final EditText password1 = findViewById(R.id.CreatePassword);
final Switch isAdmin = findViewById(R.id.isadmin);
final Button createuser = findViewById(R.id.createuserbtn);
if (getIntent().hasExtra("com.example.northlandcaps.crisis_response")){
isAdmin.setVisibility(View.GONE);
}
createuser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String username = username1.getText().toString();
final String password = password1.getText().toString();
final String isadmin = isAdmin.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response Value: ", response);
if (response.equals("success")){
Intent intent = new Intent(CreateUser.this, MainActivity.class);
CreateUser.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry",null)
.create()
.show();
}
}
};Response.ErrorListener errorListener = new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_SHORT).show();
}
};
RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener,errorListener);
RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
queue.add(registerRequest);
}
});
}
Now, the only error im getting is an Undefined index. And thats because Volley isnt sending data to the php script. The php script does work properly when data is sent to it, so my question is this; what changes do i have to make to my script for it to send the 3 strings over?
Never mess with code or else it will be confusing for you to handle things properly.
So just make another class and use it in your activity.
Have a look at this class I have written, you can use it anywhere and for any type of data request.
public class SendData {
private Context context;
private String url;
private HashMap<String, String> data;
private OnDataSent onDataSent;
public void setOnDataSent(OnDataSent onDataSent) {
this.onDataSent = onDataSent;
}
public SendData(Context context, String url, HashMap<String, String> data) {
this.context = context;
this.url = url;
this.data = data;
}
public void send(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(onDataSent != null){
onDataSent.onSuccess(response);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if(onDataSent != null){
onDataSent.onFailed(error.toString());
}
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.putAll(data);
return map;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, 0));
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
public interface OnDataSent{
void onSuccess(String response);
void onFailed(String error);
}
}
And now you can easily use it from any activity. Just give data in the constructor and use the interface to track the events this way
HashMap<String, String> data = new HashMap<>();
data.put("username", "");//define the value
data.put("password", "");//define the value
data.put("is_admin", "");//define the value
SendData sendData = new SendData(this, "", data); //defie the context and url properly
sendData.setOnDataSent(new SendData.OnDataSent() {
#Override
public void onSuccess(String response) {
//parse the response
}
#Override
public void onFailed(String error) {
//something went wrong check the error
}
});
sendData.send();
Am new to android and i have just checked out volley android library and i want to use it in my custom http request bu this fails with error in android studio at .getInstance
I have tried the following
So am using android studio and have the following folder structure
com.geowan .....
frontend
1.LoginActivity
helpers
ApiSingleTon
So in my ApiSingleton i have
public class ApiSingleton {
private static ApiSingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private ImageLoader mImageLoader;
private ApiSingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
....stuff copy pasted from volley docs on IMage disc cache loader
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
Now to my java login code i have
ApiSingleton singleton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
trylogin(); //login method
}
});
}
tryLogin(){ //trying to emulate a http request
//proceed with authentication
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, loginurl, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
Log.i(TAG, error.toString());
}
});
}
singleton.getInstance(this).addToRequestQueue(jsonObjectRequest); //fails
The above throws an error at
singleton.getInstance(this)
Where could i be going wrong.
First I guess you Need To Read More About Singleton
Second
singleton.getInstance(this)
this Means You Have A Function Called getInstance return the class type in your class
So I guess You Need to Add this
public static synchronized AppSingleton getInstance(Context context) {
if (mAppSingletonInstance == null) {
mAppSingletonInstance = new AppSingleton(context);
}
return mAppSingletonInstance;
}
According to https://developer.android.com/training/volley/requestqueue.html#singleton
it is discouraged to use the old way of implementing a singleton class by setting up the RequestQueue in Application.onCreate()
The provided "new" more modular way as seen below however doesn't contain a method for adding tags to requests and cancelling them using these tags.
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
How do I go about adding the methods similar to the ones below from the old way (in Application.onCreate()):
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
Nothing about RequestQueue has changed in that respect. You're just accessing it from a different singleton than the Application class. Add the tags to the requests the same way you always would:
ImageRequest request = new ImageRequest(...);
request.setTag(MY_TAG);
MySingleton.getInstance(this).addToRequestQueue(request);
-- edit --
Further elaboration: it's the same as if you were to get the RequestQueue object from anywhere else. It's still just a RequestQueue.
So this:
mRequestQueue.cancelAll(tag);
Becomes:
MySingleton.getRequestQueue().cancelAll(tag);
The same goes for anything else you were previously doing with a RequestQueue. The example MySingleton class there is just acting to hold on to the request queue for you. It isn't changing it.
To cancel absolutely everything, no matter what tag:
MySingleton.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() {
#Override
public boolean apply(Request<?> request) {
return true;
}
});
I am trying send post request in Json using StringRequest.I desing Jersey Servlet in java and I created android project in android studio.
I can send json object with Poster Plugin in Chrome but I cant android client.
Please help me.
Jersey Servlet:
#Path("/register")
public class RegisterServlet {
#POST
#Path("/add")
#Consumes(MediaType.APPLICATION_JSON)
public Response addUser(UserDTO user){
if((user.getUsername()=="") || (user.getEmail()=="") || (user.getPassword()=="")){
Logger.getLogger(RegisterServlet.class).info("Name or Email or Password is empty");
return Response.status(200).entity("empty").build();
}
if(Service.getInstance().addUser(user)){
Logger.getLogger(RegisterServlet.class).info("Added User --> username: "+user.getUsername()+"email: "+user.getEmail());
return Response.status(200).entity("add").build();
}else{
Logger.getLogger(RegisterServlet.class).info("Error Add User");
return Response.status(200).entity("error").build();
}
}
}
Android Code ;
private void postUser(final String username,final String email,
final String password,final String dep_name){
String tag_string_req = "req_register";
StringRequest strReq = new StringRequest(Method.POST,
"http://192.168.56.1:8080/ServerApp/register/add", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", username);
params.put("email", email);
params.put("password", password);
params.put("dep_name", dep_name);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
AppController in Android;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Request.Method.GET,
"http://example.com/", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Document document = Jsoup.parse(response);
Elements elementss = document.select("div.category > li");
for (Element element : elements) {
Elements naslov = el.select("div.text > li.headline);
}
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Greška", Toast.LENGTH_SHORT).show();
pDialog.hide();
}
});
AppController.getInstance().addToRequestQueue(strReq);
How could I pass URL parameter, instead of creating new StringRequest everytime i want to parse data, because different URLs have the same HTML structure, could I pass URL parameter when adding StringRequest to RequestQueue, something like:
AppCore.getInstance("http://example.com/").addToRequestQueue(strReq);
And use the same StringRequest several times, but with different URLs?
Also, here's my AppCore.java:
package app.android.volley;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppCore extends Application {
public static final String TAG = AppCore.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static AppCore mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppCore getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
Basically what I'm trying to do is using one StringRequest multiple times, but with different URLs.
Thanks in advance.
You can try the following way (of course, you can replace JSONArray request by JSONObject request or String request):
VolleyResponseListener listener = new VolleyResponseListener() {
#Override
public void onError(String message) {
// do something...
}
#Override
public void onResponse(Object response) {
// do something...
}
};
makeJsonArrayRequest(context, Request.Method.POST, url, requestBody, listener);
Body of makeJsonArrayRequest can be as the following:
public void makeJsonArrayRequest(Context context, int method, String url, String requestBody, final VolleyResponseListener listener) {
JSONObject jsonRequest = null;
try {
...
if (requestBody != null) {
jsonRequest = new JSONObject(requestBody);
}
...
} catch (JSONException e) {
e.printStackTrace();
}
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
listener.onResponse(jsonArray);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
listener.onError(error.toString());
}
});
// Access the RequestQueue through singleton class.
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
}
VolleyResponseListener interface as the following:
public interface VolleyResponseListener {
void onError(String message);
void onResponse(Object response);
}