Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Aim: Create A Webservice Using URL Connection in Android To Demonstrate Arithmetic Function. Activity - Main - XML

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Mohammadshafe Pahochiya 18BECE30537

Aim : Create a webservice using URL Connection in android to


demonstrate Arithmetic function.

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

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call Service"
android:id="@+id/btnCallService"/>

</RelativeLayout>

activity_addition.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".addition">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edA"
android:hint="Enter A"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edB"
android:hint="Enter B"
android:layout_below="@+id/edA"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnCall"
android:text="Add"
android:layout_below="@+id/edB"/>

</RelativeLayout>

MainActivity.java
Mohammadshafe Pahochiya 18BECE30537

package com.example.webserviceusingurlconnection;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {


Button mBtnCall;
String data;
String Default="null";
String TAG="####";
EditText username,password;
HttpURLConnection urlConnection;
String response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnCall = (Button) findViewById(R.id.btnCallService);

mBtnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});

private class AsyncCallWS extends AsyncTask<String, Void, Void>


{
@Override
protected Void doInBackground(String... params)
{
try
{
data=fetchdata();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
Mohammadshafe Pahochiya 18BECE30537

}
@Override
protected void onPostExecute(Void result)
{
Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();

}
@Override
protected void onPreExecute()
{
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values)
{
Log.i(TAG, "onProgressUpdate");
}
}

public String fetchdata()throws Exception


{
BufferedReader in=null;

String murl="http://www.easyhome.16mb.com/login.php?
u=arpit@a.com&p=12345";
try{
URL url = new URL(murl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStream inputStream = new
BufferedInputStream(urlConnection.getInputStream());
response = convertInputStreamToString(inputStream);
}
else
{
Toast.makeText(MainActivity.this,"FAIL",Toast.LENGTH_LONG).show();
//result = 0; //"Failed to fetch data!";
}

}
catch (Exception ex)
{
Toast.makeText(MainActivity.this,
ex.getMessage(),Toast.LENGTH_LONG).show();
}

return response;
}

private String convertInputStreamToString(InputStream inputStream) throws


IOException {

BufferedReader bufferedReader = new BufferedReader( new


InputStreamReader(inputStream));

String line = "";


String result = "";
Mohammadshafe Pahochiya 18BECE30537

while((line = bufferedReader.readLine()) != null){


result += line;
}

/* Close Stream */
if(null!=inputStream){
inputStream.close();
}

return result;
}
}

Addition.java
package com.example.webserviceusingurlconnection;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class addition extends AppCompatActivity {


EditText mEda, mEdb;
Button mBtnCall;
String data;
String Default="null";
String TAG="####";
EditText username,password;
HttpURLConnection urlConnection;
String response;

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

mEda = (EditText) findViewById(R.id.edA);


mEdb = (EditText) findViewById(R.id.edB);

mBtnCall = (Button) findViewById(R.id.btnCall);

mBtnCall.setOnClickListener(new View.OnClickListener() {
@Override
Mohammadshafe Pahochiya 18BECE30537

public void onClick(View v) {


AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, Void>
{
@Override
protected Void doInBackground(String... params)
{
try
{
data=fetchdata();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
Toast.makeText(addition.this, response, Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute()
{
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values)
{
Log.i(TAG, "onProgressUpdate");
}
}

public String fetchdata()throws Exception


{
BufferedReader in=null;
String murl="http://www.easyhome.16mb.com/sum.php?
one="+mEda.getText().toString()+"&two="+mEdb.getText().toString();
try{
URL url = new URL(murl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStream inputStream = new
BufferedInputStream(urlConnection.getInputStream());
response = convertInputStreamToString(inputStream);
}
else
{
Toast.makeText(addition.this,"FAIL",Toast.LENGTH_LONG).show();
//result = 0; //"Failed to fetch data!";
}
Mohammadshafe Pahochiya 18BECE30537

}
catch (Exception ex)
{
Toast.makeText(addition.this,
ex.getMessage(),Toast.LENGTH_LONG).show();
}
return response;
}

private String convertInputStreamToString(InputStream inputStream) throws


IOException {

BufferedReader bufferedReader = new BufferedReader( new


InputStreamReader(inputStream));

String line = "";


String result = "";

while((line = bufferedReader.readLine()) != null){


result += line;
}

/* Close Stream */
if(null!=inputStream){
inputStream.close();
}
return result;
}
}

Output :

You might also like