Soru & Cevap

JSON ile webten veri çekmek ...

30.11.2015 - 11:21

Merhaba Arkadaşlar bir videoda anlatılan dersi izleyerek benim eklediğim veritabanında verileri çekmek istedim fakat ekran boş geliyor. Yaptığım hatayı bulamadım yardımcı olabilirmisiniz. 

Mysql verirsi

[{"id":"1","soru":"1.Soru","cevap1":"cevap1 Alan?","cevap2":"cevap2 Alan?","cevap3":"cevap3 Alan?","cevap4":"cevap4 Alan?"}] 

package com.example.str.myapplication;

import android.content.Entity;
import android.net.wifi.WifiConfiguration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

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 java.io.IOException;


public class MainActivity extends AppCompatActivity {
final static String URL="http://localhost:81/sinav.php";
    TextView tvData;
    JSONObject json;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tvData= (TextView) findViewById(R.id.tvData);
       new Game().execute("cevap4");

    }

protected JSONObject readGameParks() 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 posts=new JSONArray(data);
        JSONObject last=posts.getJSONObject(0);
        return last;


    }

    return null;
}

    public class Game extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... params) {
            try {
                json=readGameParks();
                String data=json.getString(params[0]);
                return data;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(String data) {
            tvData.setText(data );
        }
    }


}

 

52 Görüntülenme

3 Cevap

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

picture-16490-1553546609.jpg
bektasesref
01.12.2015 - 12:08

Volley kütüphanesini kullanabilirsin. Gayet kullanışlı

Profile picture for user hayalimdesin
hayalimdesin
30.11.2015 - 04:40

Teşekkürler hocam güzel bir kaynak.

picture-28812-1452251417.jpg
umutonur
30.11.2015 - 11:55