Soru & Cevap

Android Json ...

24.03.2015 - 09:39

package com.example.tuna.yeniuygulama;

 

import android.content.Context;

import android.os.AsyncTask;

import android.os.Environment;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.TextView;

 

import org.json.JSONArray;

import org.json.JSONObject;

 

import java.io.BufferedInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

 

 

public class MainActivity extends ActionBarActivity {

    //JSON URL

    private static String url = "http://ahmeteralp.com/demorestaurant.php";

   // private static String test_url = "http://www.korfezvideo.com/korfeztest/test.php?version=1&kurs=";

    //%@&ders=%@&konu=%@&soru=%i";

    //JSON GLOBAL VARIABLE NODE NAMES

    private static final String TAG_CATEGORY = "categoty";

 

    private static final String TAG_CATEGORY_TITLE = "category_title";

    private static final String TAG_CATEGORY_İD = "category_id";

 

    //JSON ARRAY

    JSONArray datas = null;

    String zipTest = "";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        zipTest= Environment.getExternalStorageDirectory() + "category";

 

 

 

        new GetJSON().execute();

    }

 

    public class GetJSON extends AsyncTask<Void,Void,Void>

    {

        Context context;

        int layoutResourceId;

 

 

        @Override

        protected Void doInBackground(Void... params) {

            ServerHandler sh = new ServerHandler();

 

 

 

            // Making a request to url and getting response

            try {

                String jsonStr = sh.makeServerCall(url, ServerHandler.GET);

 

                if(jsonStr!=null)

                {

                    try {

                        JSONObject jsonObj = new JSONObject(jsonStr);

                        datas = jsonObj.getJSONArray(TAG_CATEGORY);

 

 

 

                        for (int i = 0; i < datas.length(); i++)

                        {

                            JSONObject c = datas.getJSONObject(i);

 

 

                            String jCATEGORY = c.getString(TAG_CATEGORY);

 

                            String jCATEGORY_ID = c.getString(TAG_CATEGORY_TITLE);

 

 

                            String jCATEGORY_TITLE = c.getString(TAG_CATEGORY_İD);

 

 

                            Log.i("category"+(i+1)+"",jCATEGORY);

 

                            Log.i("CATEGORY_ID "+(i+1)+"",jCATEGORY_ID);

                            Log.i("CATEGORY_TITLE "+(i+1)+"",jCATEGORY_TITLE);

 

 

                        }

 

                    }catch (Exception e){

 

                    }

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

 

            return null;

        }

 

 

 

    }

    private class DownloadZipFile extends AsyncTask<String, String, String>

 

 

    {

 

        @Override

        protected String doInBackground(String... zipURL) {

 

 

 

            int count = 0;

 

            URL url = null;

 

            try {

                url = new URL(zipURL[0]);

            } catch (MalformedURLException e) {

                e.printStackTrace();

            }

            URLConnection conexion = null;

 

            try {

                conexion = url.openConnection();

            }catch (IOException e) {

 

                e.printStackTrace();

            }

            try {

                conexion.connect();

            } catch (IOException e) {

                e.printStackTrace();

            }

 

 

            int lenghtOfFile = conexion.getContentLength();

            //Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

 

            InputStream input = null;

 

            try {

                input = new BufferedInputStream(url.openStream());

            } catch (IOException e) {

                e.printStackTrace();

            }

            OutputStream output = null;

            try {

                output = new FileOutputStream(zipTest);

            }catch (FileNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

 

            byte data[] = new byte[1024];

 

            long total = 0;

 

            try{

                while ((count = input.read(data)) != -1) {

                    total += count;

                    publishProgress(""+(int)((total*100)/lenghtOfFile));

                    output.write(data, 0, count);

                }

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

 

            try {

                output.flush();

            } catch (IOException e) {

                e.printStackTrace();

            }

            try {

                output.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

            try {

                input.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

 

            return null;

        }

 

 

 

 

 

 

        class CartoonsViewHolder {

            ImageView img;

            TextView baslik;

            TextView detay;

 

        }

    }

 

 

}

HandServer.java kodum

package com.example.tuna.yeniuygulama;

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.ParseException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.utils.URLEncodedUtils;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

 

import java.io.IOException;

import java.util.List;

 

/**

 * Created by aek on 27.12.14.

 */

public class ServerHandler

{

    static String response = null;

    public final static int GET = 1;

    public final static int POST = 2;

 

    public ServerHandler()

    {

 

    }

    public String makeServerCall(String url, int method) throws IOException {

        try {

            return makeServerCall(url, method, null);

        } catch (IOException e) {

            e.printStackTrace();

        }

        return null;

    }

    public String makeServerCall(String url, int method,List<NameValuePair> params) throws IOException {

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpEntity httpEntity = null;

        HttpResponse httpResponse = null;

 

        // Checking http request method type

        if (method == POST) {

            HttpPost httpPost = new HttpPost(url);

            // adding post params

            if (params != null) {

                httpPost.setEntity(new UrlEncodedFormEntity(params));

            }

 

            httpResponse = httpClient.execute(httpPost);

 

        } else if (method == GET) {

            // appending params to url

            if (params != null) {

                String paramString = URLEncodedUtils

                        .format(params, "utf-8");

                url += "?" + paramString;

            }

            HttpGet httpGet = new HttpGet(url);

 

            httpResponse = httpClient.execute(httpGet);

 

        }

        httpEntity = httpResponse.getEntity();

        try {

            response = EntityUtils.toString(httpEntity);

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ParseException e) {

            e.printStackTrace();

        }

        return response;

    }

}

http://ahmeteralp.com/demorestaurant.php üzerinden veriyi bu şekilde çekemiyorum yardım eder misiniz

1 Görüntülenme

1 Cevap

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

Profile picture for user sefaturan
sefaturan
24.03.2015 - 09:40

Buradaki phpden verileri çekemedim kodu inceleyip geri dönüş yaparmısınız turansefa55@outlook.com adresine