0% found this document useful (0 votes)
60 views6 pages

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

The document describes creating a web service in Android using URL connection to demonstrate arithmetic functions. It includes XML layout files for main and addition activities, with buttons and edit texts. The MainActivity calls a web service and displays the response. The addition activity gets two numbers from edit texts, calls a sum web service, and displays the response. The code includes an AsyncTask to call the web services and handle the responses.

Uploaded by

shafe SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views6 pages

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

The document describes creating a web service in Android using URL connection to demonstrate arithmetic functions. It includes XML layout files for main and addition activities, with buttons and edit texts. The MainActivity calls a web service and displays the response. The addition activity gets two numbers from edit texts, calls a sum web service, and displays the response. The code includes an AsyncTask to call the web services and handle the responses.

Uploaded by

shafe SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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?
[email protected]&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