Soru & Cevap

json ile veri cekimi ...

26.05.2016 - 04:21

Merhaba arkadaşlar siteden veri cekmek istedim netten örnek bir kod buldum ama ya hata aldım yada sonuc vermedi. İlk defa json kullanıcam o yüzden benim anlayabileceğim şekilde kod parçası olan varmı ? yada ders anlatımıda olur :)

538 Görüntülenme

5 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-57989-1451678910.jpg
serdarşen
04.06.2016 - 12:01

Merhaba Ebru, popüler ve hızlı json ile veri çekmek için https'yi de destekleyen, Turkcell Geleceği Yazanlarda da anlatılan Volley kütüphanesini tavsiye ederim. HttpClient ve AsyncTask kullanmana gerek kalmaz. Kolay gelsin

Profile picture for user Ebrutr
Ebrutr
27.05.2016 - 03:03
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

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

public class Parse {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = null;
    public Parse(){

    }
    public static String getJSONFromUrl(String url) throws IOException {
        Log.e("URL",url);
        try{
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }catch (UnsupportedEncodingException e){
            Log.e("JSON Parser", "UnsupportedEncodingException " + e.toString());
            e.printStackTrace();
        }catch (ClientProtocolException e){
            Log.e("JSON Parser", "ClientProtocolException " + e.toString());
            e.printStackTrace();
        }catch (IOException e){
            Log.e("JSON Parser", "IOException " + e.toString());
            e.printStackTrace();
        }
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = reader.readLine())!= null){
                sb.append(line + "\n");

            }
            is.close();
            json = sb.toString();

        }catch (Exception e){
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        return json;
    }

 

 

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class Havadurumu extends AppCompatActivity {
    TextView txt_derece;
    Button btn_goster;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_havadurumu);
        txt_derece = (TextView) findViewById(R.id.txtgoster);
        btn_goster = (Button) findViewById(R.id.btn_hava);

        btn_goster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new getWeatherCondition().execute();
            }
        });

    }
    private class getWeatherCondition extends AsyncTask<Void,Void,Void>{
        String dolar;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(Void... Params)  {
            String url = "http://www.doviz.gen.tr/doviz_json.asp?version=1.0.4";
            JSONObject jsonObject = null;
            try {
                String json =  Parse.getJSONFromUrl(url);
                try {
                    jsonObject = new JSONObject(json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    dolar = jsonObject.getString("sonkayit");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;

        }
        @Override
        protected void onPostExecute(Void args) {
            txt_derece.setText(dolar);
        }
    }

}

Merhaba arkadaşlar böyle bir şeyler çıktı ortaya çalışıyor ama sizce ne kadar verimlidir ve hızlıdır. Tavsiyelerinizi bekliyorum

picture-19298-1459404577.jpg
alper_beyler
27.05.2016 - 12:07
Ebru Çakır
27.05.2016 - 03:05
Baktım ama pek anlayamayınca aşağıdaki gibi yaptım sizce yaptıgım nasıl eksik yönleri artısı ve eksisi nedir.
picture-17431-1465777184.jpg
fatihtuzen
26.05.2016 - 11:31

projene muhtemelen kütüphane dosyalarını import etmediğin için HTTP Client kodlarında sıkıntı çıkmıştır diye düşünüyorum 

bu linkten upload ettim indirip denemeni tavsiye ederim 

http://fatihtuzen.com/dosyalar%C4%B1m/Android-Http-K%C3%BCt%C3%BCphanesi/

Ebru Çakır
26.05.2016 - 11:54
hayır sıkıntı orda değil ilk defa kullanıyorum ve nasıl bir algoritma ve nasıl yol izleyecegimi bilmiyorum . örnek bir koda ihtiyacım var anlaşılır.
Fatih TÜZEN
27.05.2016 - 12:17
Android için söylüyorum ama verileri Http kısmında çektikten sonra JsonObject nesnesi ile String bir değişken gibi kullanabilirsin örnek için yapmak istediğin ve çektiğin verilerin linkini payalaşabilirsen benzer kodlar gönderebilirm
Ebru Çakır
27.05.2016 - 01:32
internette tahmini bölyle şeyler var .
Profile picture for user mustafakucuk
mustafakucuk
26.05.2016 - 10:12

Merhabalar, hangi dil ile yapmak istiyorsunuz ?

Ebru Çakır
26.05.2016 - 11:06
java . Ugraşıyorum ama hata alıyorum hep örneklerden de baktım ama yapamadım.