Mobil uygulamalar veri tabanı, nesnelerin interneti, elektronik cihazlar gibi diğer platformlara ulaşmak için web service yapısından yararlanır. Bu makalede üçgenin alanını bulmak için hazırlanan web service projesine mobil tarafından bilgi göndererek sonucu telefonda gösteren uygulamayı anlatacağız.
Android Studio programını açalım.
Yeni bir proje oluşturalım.
Ksoap Kütüphanesini Android Studio Projesine dahil edelim.
Üçgenin alanını bulan web service adresini kontrol edelim.
Daha sonra activity_main.xml dosyasını düzenleyelim.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="A Kenarı">
<requestFocus></requestFocus>
</EditText>
<EditText
android:id="@+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="B Kenarı" />
<EditText
android:id="@+id/txtC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="C Kenarı" />
<Button
android:id="@+id/btnAlan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="CallWebservice"
android:text="Hesapla" />
<TextView
android:id="@+id/txtSonuc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Şimdi de ServiceManager.java isminde bir sınıf oluşturalım ve aşağıdaki kodları yazalım.
package com.example.webservisucgenalani;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
/**
* Created by HP on 4.06.2017.
*/
public class ServiceManager {
private static final String METHOD_NAME = "alanHesapla";
private static final String NAMESPACE = "http://www.abc.com/";
private static final String SOAP_ACTION = "http://www.abc.com/alanHesapla";
private static final String URL = "http://atml.somee.com/uyelik.asmx";
SoapObject soapObject;
SoapSerializationEnvelope soapSerializationEnvelope;
HttpTransportSE httpTransportSE;
public String PushData(String a, String b, String c) {
soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("a", a);
soapObject.addProperty("b", b);
soapObject.addProperty("c", c);
soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapSerializationEnvelope.dotNet = true;
soapSerializationEnvelope.setOutputSoapObject(soapObject);
httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;
String deger;
try {
httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
SoapPrimitive soapPrimitive = (SoapPrimitive) soapSerializationEnvelope.getResponse();
return soapPrimitive.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return "son";
}
}
Şimdi de MainActivity.java dosyasını düzenleyelim.
package com.example.webservisucgenalani;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText a, b, c;
Button btnAlan;
TextView txtSonuc;
ServiceManager serviceManager;
String sonuc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (EditText) findViewById(R.id.txtA);
b = (EditText) findViewById(R.id.txtB);
c = (EditText) findViewById(R.id.txtC);
btnAlan = (Button) findViewById(R.id.btnAlan);
txtSonuc= (TextView) findViewById(R.id.txtSonuc);
serviceManager=new ServiceManager();
}
public void CallWebservice(View view) {
if (a.length() > 0 && b.length()>0 && c.length()>0) {
getCelsius(a.getText().toString(),b.getText().toString(),c.getText().toString());
} else {
txtSonuc.setText("Lütfen Değer Giriniz");
}
a.setText("");
b.setText("");
c.setText("");
a.requestFocus();
}
private final void getCelsius(final String x,final String y,final String z) {
new Thread(new Runnable() {
@Override
public void run() {
sonuc = serviceManager.PushData(x,y,z);
handler.sendEmptyMessage(0);
}
}).start();
}
public Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
txtSonuc.setText(sonuc);
break;
}
return false;
}
});
}
Son olarak AndroidManifest.xml dosyasını düzenleyelim.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WebServisUcgenAlani"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Artık projemizi çalıştırabiliriz.
|