requester.ExchangeRequest.java Source code

Java tutorial

Introduction

Here is the source code for requester.ExchangeRequest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package requester;

import android.os.StrictMode;

import static android.provider.Browser.BookmarkColumns.URL;
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;

/**
 *
 * @author ertan
 */
public class ExchangeRequest {

    private String requestURL = "http://www.doviz.gen.tr/doviz_json.asp?version=1.2";
    private static String dolarBuyingRate = "";
    private static String dolarSellingRate = "";
    private static String euroBuyingRate = "";
    private static String euroSellingRate = "";

    public static String lastUpdateTime = "";

    public ExchangeRequest() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    public void setRequestURL(String aRequestURL) {
        this.requestURL = aRequestURL;
    }

    public String getRequestURL() {
        return requestURL;
    }

    public void getExchangeResult() {
        JSONObject resultObj = null;

        HttpClient client = new DefaultHttpClient();
        HttpGet getData = new HttpGet(requestURL);
        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
                resultObj = new JSONObject(result);

                setDolarBuyingRate(resultObj.getString("dolar"));
                setEuroBuyingRate(resultObj.getString("euro"));

                setDolarSellingRate(resultObj.getString("dolar2"));
                setEuroSellingRate(resultObj.getString("euro2"));

                lastUpdateTime = resultObj.getString("guncelleme");
                //LastRegisterDate = jsonVeri.getString("sonkayit");
            }

        } catch (Exception e) {
            e.printStackTrace();
            lastUpdateTime = "Gncelleme basarisiz!\nInternet baglantinizi kontrol edip, tekrar deneyiniz!";

        }
    }

    public String getEuroBuyingRate() {
        return euroBuyingRate;
    }

    public String getEuroSellingRate() {
        return euroSellingRate;
    }

    public String getDolarBuyingRate() {
        return dolarBuyingRate;
    }

    public String getDolarSellingRate() {
        return dolarSellingRate;
    }

    public void setEuroBuyingRate(String aBuyingEuro) {
        this.euroBuyingRate = aBuyingEuro;
    }

    public void setEuroSellingRate(String aSellingEuro) {
        this.euroSellingRate = aSellingEuro;
    }

    public void setDolarBuyingRate(String aBuyingDolar) {
        this.dolarBuyingRate = aBuyingDolar;
    }

    public void setDolarSellingRate(String aSellingDolar) {
        this.dolarSellingRate = aSellingDolar;
    }

    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();
    }

}