Soru & Cevap

Android siteden veri çekme ...

06.09.2014 - 08:09

Uygulamamda worldometers.info sitesinden sürekli güncellenen sayıları göstermek istiyorum ama bir türlü nasıl yapabileceğimi anlayamadım json ile text çekebiliyoruz ama bunda nasıl kullanabiliceğimi bilemiyorum bnm kullanmaya çalıştığım kodlar ( videolu anlatımda vardı) ;

package com.whatishappening;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
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 org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class Veri extends Activity {
    
    final static String URL = "http://www.worldometers.info/";
    TextView tvdata;
    JSONObject json;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvdata = (TextView) findViewById(R.id.tvdata);
        new game().execute("text");
    }
    protected JSONObject worlddometers() throws ClientProtocolException, IOException, JSONException{
        
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URL);
        HttpResponse response = client.execute(get);
        StatusLine status  = response.getStatusLine();
        int s = status.getStatusCode();
        
        if(s == 200){
            
            HttpEntity e = response.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray post = new JSONArray(data);
            JSONObject last = post.getJSONObject(0);
            return last;
                                    
        }
        
        
        return null;
        
    }
    
    public class game extends AsyncTask<String, String , String >{

        @Override
        protected String doInBackground(String... params) {
            
            try {
                json = worlddometers();
                String data = json.getString(params[0]);
                return data;
                
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                        
            
            return null;
        }
        
        @Override
        protected void onPostExecute(String data){
            
            tvdata.setText(data);
            
            
        }
        
        
    }

}

21 Görüntülenme

1 Cevap

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

picture-21102-1406987710.png
kerimaydinceng
03.12.2014 - 02:33

Sorun çözülmediyse browser key ine bakmanı öneririm ,onun yanlış kullanımı veya alımında bu tür sorunlar çıkabiliyor,biraz geç bir cevap  ama başka uygulamalarda da karşına çıkabilir bu sorun  :)