Merhaba Gökhan,
.Net ile yazılmış bir servis ile ekleme ve listeleme işlemlerini yapan bir örneğim mevcut aşağıda kodları paylaşıyorum.
İşini görmesi dileğiyle.
AndroidManifest.xml ( Burada interneti kullanma iznini ekliyoruz.)
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.webservis">
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".Ekle">
<intent-filter>
<action android:name="android.intent.action.EKLE"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
<activity android:name=".Listele">
<intent-filter>
<action android:name="android.intent.action.LISTELE"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ekle"
android:id="@+id/btnEkle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="147dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listele"
android:id="@+id/btnListele"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/btnEkle"
android:layout_alignStart="@+id/btnEkle" />
</RelativeLayout>
ekle_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etAdiSoyadi"
android:hint="Adi Soyadi"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etEposta"
android:hint="Eposta"
android:layout_below="@+id/etAdiSoyadi"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ekle"
android:id="@+id/bEkle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
listele_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lvBilgiler"
android:layout_gravity="center_horizontal"
android:background="@color/material_grey_900" />
</LinearLayout>
MainActivity.java
package com.webservis;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by zafercinar
*/
public class MainActivity extends Activity {
Button btnEkle,btnListele;
View.OnClickListener Listener;
String Url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
install_elements();
}
private void install_elements()
{
btnEkle = (Button) findViewById(R.id.btnEkle);
btnListele = (Button) findViewById(R.id.btnListele);
Url = "http://www.zafercinar.com/";
Listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btnEkle: activity(Ekle.class); break;
case R.id.btnListele: activity(Listele.class); break;
default: break;
}
}
};
btnEkle.setOnClickListener(Listener);
btnListele.setOnClickListener(Listener);
}
private void activity(Class c)
{
Intent i = new Intent(MainActivity.this,c);
i.putExtra("Url",Url);
startActivity(i);
}
}
Listele.java
package com.webservis;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by zafercinar
*/
public class Listele extends Activity {
ArrayAdapter<String> adapter;
String resp;
ArrayList<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listele_layout);
new AspData().execute();
}
public class AspData extends AsyncTask<String,String,String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
DefaultHttpClient hc = new DefaultHttpClient();
ResponseHandler response = new BasicResponseHandler();
HttpGet http = new HttpGet(getIntent().getStringExtra("Url") + "uyelik.asmx/getUyeler?");
try{
resp = (String) hc.execute(http, response);
resp = cutStr(resp);
JSONArray json = new JSONArray(resp);
for (int i=0;i<json.length();i++)
{
JSONObject e = json.getJSONObject(i);
data.add(e.getString("AdiSoyadi") + " - " + e.getString("Eposta"));
}
adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,data);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
ListView lv = (ListView) findViewById(R.id.lvBilgiler);
lv.setAdapter(adapter);
}
}
private String cutStr(String str)
{
str = str.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
str = str.replace("<string xmlns=\"http://tempuri.org/\">","");
str = str.replace("</string>", "");
str = str.substring(2,str.length());
return str;
}
}
Ekle.java
package com.webservis;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by zafercinar
*/
public class Ekle extends Activity {
EditText etAdiSoyadi,etEposta;
Button bEkle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ekle_layout);
install_elements();
bEkle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// servisimizi burada çalıştıracağız
new Servis().execute(etAdiSoyadi.getText().toString(),etEposta.getText().toString());
}
});
}
class Servis extends AsyncTask<String,String,String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet();
String adsoyad = params[0];
String eposta = params[1];
request.setURI(new URI(getIntent().getStringExtra("Url") + "uyelik.asmx/uyeEkle?AdiSoyadi="+adsoyad+"&Eposta="+eposta));
if(!httpClient.execute(request).equals(null))
{
ToastYazdir("Eklendi");
Thread.sleep(1500);
Intent i = new Intent(Ekle.this,Listele.class);
i.putExtra("Url", getIntent().getStringExtra("Url"));
//startActivity(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
private void ToastYazdir(final String str)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
public void install_elements()
{
etAdiSoyadi = (EditText) findViewById(R.id.etAdiSoyadi);
etEposta = (EditText) findViewById(R.id.etEposta);
bEkle = (Button) findViewById(R.id.bEkle);
}
}