1

I am trying to use Volley and send GET request to check data after login.
I have special class 'API' where's all Volley staff.
What I am trying to do is after checking validation in LoginActivity (default android studio template) I want to create API instance in LoginActivity and send GET request to server.
This is only for testing Volley, later I want to create one API instance in some Main class.

Here's my API class:

import android.app.Application;
import android.util.Log;

import com.android.volley.*;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
import java.lang.String;

public class API extends Application {

    protected String ServerURL;
    protected String GET;
    protected String POST;
    protected RequestQueue queue;

    public API(){
        setServerURL("http://localhost:63424");
        setGET("http://localhost:63424/api");
        setPOST("http://localhost:63424/api");
        queue = Volley.newRequestQueue(this);
    }

    protected void SendGET (String request)
    {
        JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, request, null,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response) {
                        // display response
                        Log.d("ResponseAPI", response.toString());
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Error.ResponseAPI", error.toString());
                    }
                }
        );
        queue.add(getRequest);
    }


    //Logowanie
    public boolean CheckLogin (String Login, String Password){
        setGET(getGET()+"/Search_Users_/login=/"+Login + "/");
        SendGET(getGET());

        return true;

    }

    public String getServerURL() {
        return ServerURL;
    }

    public void setServerURL(String ServerURL) {
        ServerURL = ServerURL;
    }

    public String getGET() {
        return GET;
    }

    public void setGET(String GET) {
        this.GET = GET;
    }

    public String getPOST() {
        return POST;
    }

    public void setPOST(String POST) {
        this.POST = POST;
    }

}

Here I am trying to create API instance in LoginActivity:

 public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String mEmail;
        private final String mPassword;

        UserLoginTask(String email, String password) {
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.

            try {
                // Simulate network access.
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return false;
            }
            Log.d("API", "Zaraz utworze API");
            API api = new API();
            return api.CheckLogin(mEmail, mPassword);

            // TODO: register the new account here.

        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;
            showProgress(false);

            if (success) {
                finish();
            } else {
                mPasswordView.setError(getString(R.string.error_incorrect_password));
                mPasswordView.requestFocus();
            }
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
            showProgress(false);
        }
    }

This solution do not work, I get an error:

FATAL EXCEPTION: AsyncTask #1
                                                            Process: pawel.cooker, PID: 2646
                                                            java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                at android.os.AsyncTask$3.done(AsyncTask.java:353)
                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                at java.lang.Thread.run(Thread.java:764)
                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
                                                                at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:256)
                                                                at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
                                                                at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
                                                                at pawel.cooker.API.<init>(API.java:17)
                                                                at pawel.cooker.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:320)
                                                                at pawel.cooker.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:299)
                                                                at android.os.AsyncTask$2.call(AsyncTask.java:333)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                                at java.lang.Thread.run(Thread.java:764) 

There's a problem with Volley, but have no idea what's wrong.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nedvid
  • 57
  • 1
  • 2
  • 12

1 Answers1

5

There's problem with Volley

No, there's a problem with your API class.

Make sure you put it in the manifest

<application
    android:name=".API"

After that

  1. Don't use a constructor. Use onCreate . OR, don't use Application, and use a Volley-scoped singleton -- see example from documentation
  2. Replace localhost with the actual Server address

public class API extends Application {

    public static final String ServerURL = "http://<Use real server here>:63424";
    public static final String GET = ServerURL+"/api";
    public static final String POST = ServerURL+"/api";
    protected RequestQueue queue;

    private final API mInstance;

    private API() { }

    public API getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        mInstance = this;
        queue = Volley.newRequestQueue(this);
    }
  1. You can't return blocking value from Async functions. Use the callbacks to pass back the results

protected void sendGET (String url, Response.Listener<JSONObject> onSuccess)
{
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            onSuccess,  
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.ResponseAPI", error.toString());
                }
            }
    );
    queue.add(getRequest);
}

public void checkLogin (String Login, Response.Listener<JSONObject> onSuccess){
    SendGET(GET+"/Search_Users_/login=/"+Login, onSuccess);
}
  1. Don't use new API() when you should instead use ((API) getApplicationContext()) or with the above implementation, API.getInstance()

For example,

API api = API.getInstance();
api.checkLogin(mEmail, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        // TODO: register the new account here.
    }
});
  1. Most importantly here, you don't need an AsyncTask when using Volley. It's already asynchronous

On a related note, if you are using RESTful APIs, then Retrofit might be more useful

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245