Aim: Create A Webservice Using URL Connection in Android To Demonstrate Arithmetic Function. Activity - Main - XML
Aim: Create A Webservice Using URL Connection in Android To Demonstrate Arithmetic Function. Activity - Main - XML
Aim: Create A Webservice Using URL Connection in Android To Demonstrate Arithmetic Function. Activity - Main - XML
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;
mBtnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
}
@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");
}
}
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;
}
/* 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addition);
mBtnCall.setOnClickListener(new View.OnClickListener() {
@Override
Mohammadshafe Pahochiya 18BECE30537
}
catch (Exception ex)
{
Toast.makeText(addition.this,
ex.getMessage(),Toast.LENGTH_LONG).show();
}
return response;
}
/* Close Stream */
if(null!=inputStream){
inputStream.close();
}
return result;
}
}
Output :