Volley json post request android

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.

Find your build.gradle (app) add this under dependencies

implementation 'com.android.volley:volley:1.2.0'

Create a singleton class to manage all requests

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AppHttp {
    private static AppHttp instance;
    private RequestQueue requestQueue;
    private static Context ctx;

    private AppHttp(Context context) {
        ctx = context;
        requestQueue = getRequestQueue();
    }

    public static synchronized AppHttp getInstance(Context context) {
        if (instance == null) {
            instance = new AppHttp(context);
        }
        return instance;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
        }
        return requestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }
}

Create your POST request

AppHttp requestQueue = AppHttp.getInstance(this);
JSONObject postData = new JSONObject();
    try {
        postData.put("email", email);
        postData.put("password", password);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://api.com/login", postData, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.e("login response: ", response.toString());
            try {
                // Retrieve user information and take user to next activity
               // after successful login
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("login error: ", error.toString());
                error.printStackTrace();

                // show user login error with a Toast
            }
        }
    ) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Accept", "application/json;charset=utf-8");
            params.put("Content-Type", "application/json;charset=utf-8");

            return params;
        }
    };

    requestQueue.addToRequestQueue(jsonObjectRequest);
}

No Builds Available on TestFlight

You may find this issue when adding a new tester on the TestFlight page, question is how this user going to get the invitation.

Suppose you are adding a tester for version 2.4 and the error message looks like this.

All you have to do is go to the previous version 2.3 and add the new tester on that version. After the user accepts the invitation you won’t see the error for that user for any new versions.

Laravel Sanctum Tutorial Step by Step

Laravel Sanctum Tutorial Step by Step

Laravel Sanctum provides a featherweight authentication system for single page applications, mobile applications, and simple, token based APIs. Sanctum allows each user of your application to generate multiple API tokens for their account. These tokens may be granted abilities / scopes which specify which actions the tokens are allowed to perform.

Installation

composer require laravel/sanctum and composer update

Next, you should publish the Sanctum configuration and migration files using the vendor:publish Artisan command. 

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate 

You can also use following sql for creating table, if there is a problem to run migrate command.

CREATE TABLE `personal_access_tokens` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
   `tokenable_type` varchar(255) NOT NULL,
   `tokenable_id` bigint(20) unsigned NOT NULL,
   `name` varchar(255) NOT NULL,
   `token` varchar(64) NOT NULL,
   `abilities` text DEFAULT NULL,
   `last_used_at` timestamp NULL DEFAULT NULL,
   `created_at` timestamp NULL DEFAULT NULL,
   `updated_at` timestamp NULL DEFAULT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `personal_access_tokens_token_unique` (`token`),
   KEY `personal_access_tokens_tokenable_type_tokenable_id_index` (`tokenable_type`,`tokenable_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

Next, if you plan to utilize Sanctum to authenticate an SPA, you should add Sanctum’s middleware to your api middleware group within your application’s app/Http/Kernel.php file:

'api' => [
            'throttle:60,1',
            'bindings',
            \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        ],
Laravel Sanctum Tutorial Step by Step

Download source code