Android POST Request In Webview Application


Webview in Android enables developers to integrate mobile friendly layouts of webpages to be implemented in an application. One such application of webivew is that it also incorporates use of POST request. This allows developers to directly send users to a webpage with certain data preloaded. eg if your app wants to send users to a webpage with payment options, you might want to send the users to that page, in an already logged in form. For this you have to POST the login parameters first. THus using webview you will be able to execute this


Overview of project files:


Design a simple layout with a Webview widget only

activity_main.xml:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android.support.constraint.ConstraintLayout
>


Lets code the java file for this activity.

First declare certain variables 

public static final String urllocal="http://192.168.0.7/kk/file.php";
private static final String VAR_field1="val",VAR_field2="val1";
private static final String val1="1",val2="1";

private WebView webView;

private String parametersToPost;


Here the urllocal is the url for the PHP file contating the server code, VAR_field1 and VAR_field2 are two static parameters for POST request. val1 and val2 are values associated with the VAR_field1 and VAR_field2 respectively. 

Next, instantiate the webview

webView=(WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());


Create a UTF-8 format string to be able to be used in POST request

try {
parametersToPost= URLEncoder.encode(VAR_field1,"UTF-8") + "=" + URLEncoder.encode(val1,"UTF-8")+"&" +
URLEncoder.encode(VAR_field2,"UTF-8") + "=" + URLEncoder.encode(val2,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


Finally load the parameters and url in webview using command:

webView.postUrl(urllocal,parametersToPost.getBytes());


Complete Java code: MainActivity.java

package com.ourcoaching.webviewpostapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

public static final String urllocal="http://192.168.0.7/kk/imageUpload/file.php";
private static final String VAR_field1="val",VAR_field2="val1";
private static final String val1="1",val2="1";

private WebView webView;

private String parametersToPost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=(WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());

try {
parametersToPost= URLEncoder.encode(VAR_field1,"UTF-8") + "=" + URLEncoder.encode(val1,"UTF-8")+"&" +
URLEncoder.encode(VAR_field2,"UTF-8") + "=" + URLEncoder.encode(val2,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
webView.postUrl(urllocal,parametersToPost.getBytes());
}
}


Download the code from GitHub



Related Tutorials