Web Servis ile Güncel Döviz Kuru Bilgilerini Almak ve Kur Dönüsümü

Merhabalar

Bu makalede internetten güncel olarak döviz kurlarini takip eden bir uygulama gelistirecegiz.

Bilgilerimizi http://www.doviz.gen.tr sitesinden web servis ile alacagiz. Verilerimizi JSON olarak siteden çagirip JSON objesi olarak parse edecegiz. Daha sonra aldigimiz verileri ekrana yansitacagiz. Aldigimniz bu verilerle  Tl-Dolar, TL-Euro çevirme uygulamasi gelistirecegiz.

Uygulamanin kaynak koduna buradan ulasabilirsiniz.

Öncelikle layoutumuzu ayarliyoruz.

doviz_kuru.xml

<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"
    android:background="#FFFFFF"
    android:clickable="false"
    android:longClickable="false"
    tools:context=".DovizKuru" >

    <TextView
        android:id="@+id/textViewTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="82dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_toRightOf="@+id/textViewZaman"
        android:text="Dolar"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:layout_toRightOf="@+id/textViewZaman"
        android:text="Euro"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/buttonGnc" >

        <TextView
            android:id="@+id/textViewTime1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" 
             android:textSize="10sp"/>

    </RelativeLayout>

    <EditText
        android:id="@+id/editTextEuro1"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:clickable="false"
        android:ems="10"
        android:enabled="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:linksClickable="false"
        android:longClickable="false" />

    <EditText
        android:id="@+id/editTextDolar1"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/editTextEuro1"
        android:clickable="false"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:longClickable="false" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/editTextDolar1"
        android:text="Alis"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:layout_below="@+id/textViewZaman"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/editTextDolar1"
        android:text="Satis"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editTextEuro2"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextEuro1"
        android:layout_alignBottom="@+id/editTextEuro1"
        android:layout_alignLeft="@+id/textView4"
        android:clickable="false"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:longClickable="false" />

    <Button
        android:id="@+id/buttonUpt"
        android:layout_width="230dp"
        android:layout_height="20dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/whitebutton"
        android:text="Güncelle" />

    <EditText
        android:id="@+id/editTextDolar2"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextDolar1"
        android:layout_alignBottom="@+id/editTextDolar1"
        android:layout_alignLeft="@+id/textView4"
        android:clickable="false"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:longClickable="false" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignParentRight="true"
        android:ems="10"
         android:inputType="number" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editTextEuro1"
        android:layout_marginTop="71dp"
        android:text="TL    :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView6"
        android:ems="10"
         android:inputType="number" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText2"
        android:ems="10"
         android:inputType="number" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText3"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignRight="@+id/textView6"
        android:text="Euro :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp"
        android:text="Dolar:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/buttonConvert"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/buttonGnc"
        android:layout_alignParentBottom="true"
             android:background="@drawable/whitebutton"
        android:text="Çevir" />

    <Button
        android:id="@+id/buttonExit"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/editTextEuro2"
        android:layout_alignParentBottom="true"
        android:background="@drawable/whitebutton"
        android:text="Çikis" />

   

</RelativeLayout>

Kodlarimizi yazmadan once layoutta kullanmis oldugum "whitebutton"tasarimini olusturmaliyiz. Bunun için projemizde "res/drawable" klasörünün içine "whitebutton.xml " olarak yeni bir xml dosyasi açiyoruz.

whitebutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape 
android:shape="rectangle" android:padding="10dp">

 <solid android:color="#ffa500"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
  android:topLeftRadius="10dp"
  android:topRightRadius="10dp"
  android:color="#ABABAB"/>
    <stroke
        android:width="2dp"
        android:color="#FF000000"/>
    
</shape>
</item>
<item>
    <shape 
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#ffffff"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
  android:topLeftRadius="10dp"
  android:topRightRadius="10dp"
  android:color="#ABABAB"/>
    <stroke
        android:width="2dp"
        android:color="#FF000000"/>
    
</shape>
    
</item>

</selector>

Simdi gerekli olan kodlarimizi yazacagiz

DovizKuru.java

package com.example.dovizkuru;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DovizKuru extends Activity {
	public static final String URL = "http://www.doviz.gen.tr/doviz_json.asp?version=1.0.4";  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		setContentView(R.layout.doviz_kuru);
		// Gerekli olan widgetlarimizi tanittik
		final EditText tlcon=(EditText)findViewById(R.id.editText1); 
		final EditText dolarcon=(EditText)findViewById(R.id.editText2); 
		final EditText eurocon=(EditText)findViewById(R.id.editText3);
	
		//program baslatildiginda verilerin güncellenmesi için kullandik
		UpdateData();
		//Güncelle butonunun dinleyicisi
		Button btnupt=(Button)findViewById(R.id.buttonGnc);
		btnupt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UpdateData();
				
			}
		});
		 final EditText dolar_1=(EditText)findViewById(R.id.editTextDolar1); 
		   final EditText dolar_2=(EditText)findViewById(R.id.editTextDolar2); 
		   final EditText euro_1=(EditText)findViewById(R.id.editTextEuro1); 
		   final EditText euro_2=(EditText)findViewById(R.id.editTextEuro2);
		   TextView UptadeTime = (TextView) findViewById(R.id.textViewTime1);
//NumberFormatException hatasi almamak için birim çeviri kismindaki EditText degerlerini "0" atiyoruz
		tlcon.setText("0");
		dolarcon.setText("0");
		eurocon.setText("0");
		final float tlcon1=Float.parseFloat(tlcon.getText().toString());
		final float dolarcon1=Float.parseFloat(dolarcon.getText().toString());
		final float eurocon1=Float.parseFloat(eurocon.getText().toString());
	//çeviri tusunun dinleyicisi
		Button btnconvert =(Button)findViewById(R.id.buttonConvert);
	btnconvert.setOnClickListener(new View.OnClickListener() {
		
		@Override
	//Birimleri birbirine çevirmek için kullanilan kodlar
		public void onClick(View v) {
			float tlcon1=Float.parseFloat(tlcon.getText().toString());
			float dolarcon1=Float.parseFloat(dolarcon.getText().toString());
			float eurocon1=Float.parseFloat(eurocon.getText().toString());
			
			if(tlcon1!=0&&dolarcon1==0&&eurocon1==0){
				dolarcon1=tlcon1/Float.parseFloat(dolar_1.getText().toString());
				eurocon1=tlcon1/Float.parseFloat(euro_1.getText().toString());
				dolarcon.setText(Float.toString(dolarcon1));
				eurocon.setText(Float.toString(eurocon1));
				
			}
			else if(dolarcon1!=0&&tlcon1==0&&eurocon1==0){tlcon1=dolarcon1*Float.parseFloat(dolar_2.getText().toString());
			        eurocon1=dolarcon1*Float.parseFloat(dolar_2.getText().toString())/Float.parseFloat(euro_1.getText().toString());
			    	tlcon.setText(Float.toString(tlcon1));
			    	eurocon.setText(Float.toString(eurocon1));}
			//Eger birden fazla birim girildiyse yanlis deger almamak icin EditTextleri sifirliyoruz
			else if(eurocon1!=0&&tlcon1==0&&dolarcon1==0){tlcon1=eurocon1*Float.parseFloat(euro_2.getText().toString());
			       dolarcon1=eurocon1*Float.parseFloat(euro_2.getText().toString())/Float.parseFloat(dolar_1.getText().toString());
			   	tlcon.setText(Float.toString(tlcon1));
				eurocon.setText(Float.toString(eurocon1));}else{
					tlcon.setText("0");
					dolarcon.setText("0");
					eurocon.setText("0");}
		}
		
	});
		//Çikis tusunun dinleyicisi
		Button btnexit=(Button)findViewById(R.id.buttonExit);
		btnexit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DovizKuru.this.finish();
			}
		});
		
	}


	
	public   void UpdateData(){
		//loyout uzerindeki bilesenleri tanimlayacagiz
		   EditText dolar_1=(EditText)findViewById(R.id.editTextDolar1); 
		   EditText dolar_2=(EditText)findViewById(R.id.editTextDolar2); 
		   EditText euro_1=(EditText)findViewById(R.id.editTextEuro1); 
		   EditText euro_2=(EditText)findViewById(R.id.editTextEuro2);
		   TextView UptadeTime = (TextView) findViewById(R.id.textViewTime1);
		   
		   
		   
		   String dolar ;
	    String euro   ;
		   String dolar2 ;
		     String euro2 ;
		    String LastUptadeTime;  
		     String LastRegisterDate ;
		//burada bir web istegi olusturuyoruz
		HttpClient client =new DefaultHttpClient();
		HttpGet  getData =new HttpGet(URL);
		HttpResponse response;
		try{
			
			response=client.execute(getData);
			HttpEntity entity=response.getEntity();
			if(entity != null){
				 
				InputStream Data=entity.getContent();
				String result =convertStreamToString(Data);
				//Siteden gelen veriyi JSON Objemize atiyoruz
				JSONObject jsonVeri=new JSONObject(result); 
				dolar = jsonVeri.getString("dolar2");  
				euro = jsonVeri.getString("euro2");  
                dolar2 = jsonVeri.getString("dolar");  
                euro2 = jsonVeri.getString("euro");  
                LastUptadeTime = jsonVeri.getString("guncelleme");  
                LastRegisterDate = jsonVeri.getString("sonkayit"); 
              
                  //Verilerimizi ekrana yazdiriyoruz
                   dolar_1.setText(dolar.toString());  
                   euro_1.setText(euro.toString());  
                   dolar_2.setText(dolar2.toString());  
                   euro_2.setText(euro2.toString());  
                   UptadeTime.setText(LastUptadeTime.toString()+"/n"+LastRegisterDate.toString());  
                
                  
                  
                 }
			
		}catch(Exception e){ LastUptadeTime="Güncelleme basarisiz!\nInternet baglantinizi kontrol edip, tekrar deneyiniz!";  
		
         }
		}
	public static String convertStreamToString(InputStream is){
		//burada gelen veriyi string degerine çevirecegiz
		 BufferedReader reader = new BufferedReader(new InputStreamReader(is));  
	        StringBuilder sb = new StringBuilder();  
	        String line = null;  
	        try {  
	          while ((line = reader.readLine()) != null) {  
	              sb.append(line).append("\n");  
	          }  
	        } catch (IOException e) {  
	        } finally {  
	          try {  
	              is.close();  
	          } catch (IOException e) {  
	          }  
	        }  
	        return sb.toString();  
	    }  
			
	 }

Son olarak  uygulamamizin interneti kullanabilmesi için bir adet permission olusturup Internet kullanimina izin vermemiz gerekiyor. AndroidManifest.xml dosyasinda "</application>" 'in altina asagidaki kodu yaziyoruz.

<uses-permission android:name="android.permission.INTERNET"/>

Bunu yaptiktan sonra uygulamamiz kullanima hazirdir.

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dovizkuru"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.dovizkuru.DovizKuru"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

Uygulamanin ekran görüntüleri:

 

 




 

 

Tags:

One thought on “Web Servis ile Güncel Döviz Kuru Bilgilerini Almak ve Kur Dönüsümü

Bir cevap yazın

E-posta hesabınız yayımlanmayacak.