aybüke
aybüke
İstanbul-II (Anadolu)
08/01/2015 tarihinden beri üye
645 GY Puanı
8K GY Sırası

Kişisel Sayfaları

İlgi Alanları

2 Rozet
0 Sertifika
1 Soru Sordu
1 Cevap Verdi
0 Blog Yazısı
0 Etiket Takibi

Hakkında

İş Tecrubesi

Kullanıcıya ait İş tecrübesi bilgisi bulunmamaktadır.

Eğitim Geçmişi

İstanbul Teknik Üniversitesi
| Aralık 2020 - Aralık 2020

Sertifikalar & Başarılar

GY Sertifikaları (0)
Kullanıcının GY sertifikası bulunmamaktadır.
Diğer Sertifikaları (0)
Kullanıcıya ait sertifika bulunmamaktadır.
Test Sonuçları (0)

Kullanıcıya ait test sonucu bulunmamaktadır.

Dil Becerileri

Son Forum Aktiviteleri

2
Tümünü Gör

Mac ile JSON'dan veri çekemiyorum

Arkadaşlar merhaba. Mac ile JSON'dan basit bir veri çekmek istiyorum ama Mac üzerinde çalışmıyor. Windows üzerinde çalışan kodlar benim bilgisayarımda çalışmıyor. İnternet izni de verdim ayrıca.

ALDIĞIM HATALAR İSE ŞÖYLE; 

java.lang.NullPointerException: lock == null

org.json.JSONException: End of input at character 0 of

MainActivity.java KLASÖRÜM İSE ŞU ŞEKİLDE;

public class MainActivity extends ActionBarActivity { 

    TextView txt;

    Button btn;

    

    String adi="";

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        btn=(Button) findViewById(R.id.button1);

        txt=(TextView) findViewById(R.id.textView1);

        

        btn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                new veriler().execute();

            }

        });

    }

 

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

        @Override

        protected String doInBackground(String... params) {

            // TODO Auto-generated method stub

            JSonParseSinifi nesne=new JSonParseSinifi();

            JSONObject json;

            json=nesne.JSonObjeAl("http://localhost/android_connect/create_product.php");

            try {

                    adi=json.getString("success");

            } catch (JSONException e) {

                // TODO Auto-generated catch block

                Log.e("Catch bloğu", e.toString());

            }

            return null;

        }

        @Override

        protected void onPostExecute(String result) {

            // TODO Auto-generated method stub

            super.onPostExecute(result);

            txt.setText(adi);

        }

    }

Aldığım bir başka hata ise;

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference

 

JSONPARSER sınıfım ise hazır bir sınıf zaten çalışan bir sınıf. Nerede hata olduğunu çözemedim günlerdir. Yardımcı olabilirseniz çok sevinirim teşekkür ederim. 

 

picture-41044-1420755027.jpg
9 yıl 2 ay önce yanıtladın

Json'dan Webview'e url çekmek

picture-41044-1420755027.jpg
17 Nisan 2016 tarihinde cevaplandı

 

package com.example.britishtower;

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

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

import android.util.Log;

public class JSonParseSinifi {

    static InputStream gelenVeriler = null;
    static JSONArray jDizi = null;
    static JSONObject jObje = null;
    static String okunanDeger = "";


    public JSONArray JSONDiziAl(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            gelenVeriler = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader oku = new BufferedReader(new InputStreamReader(
                    gelenVeriler, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String satir = null;
            while ((satir = oku.readLine()) != null) {
                sb.append(satir + "\n");
            }
            gelenVeriler.close();
            okunanDeger = sb.toString();
        } catch (Exception e) {
            Log.e("jparse", "Buffer Hatası :  " + e.toString());
        }
        try {
            jDizi = new JSONArray(okunanDeger);
        } catch (JSONException e) {
            Log.e("jparse", "Dizi Parse işlemi hatası " + e.toString());
        }
        return jDizi;
    }

    public JSONObject JSonObjeAl(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            gelenVeriler = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader oku = new BufferedReader(new InputStreamReader(
                    gelenVeriler, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String satir = null;
            while ((satir = oku.readLine()) != null) {
                sb.append(satir + "\n");
            }
            gelenVeriler.close();
            okunanDeger = sb.toString();
        } catch (Exception e) {
            Log.e("jparse", "Buffer Hatası :  " + e.toString());
        }
        try {
            jObje = new JSONObject(okunanDeger);
        } catch (JSONException e) {
            Log.e("jparse", "Obje Parse işlemi hatası " + e.toString());
        }
        return jObje;
    }

}

Tam olarak senin koduna entegre edemem ama benim hazırladığım bir hazır sınıf var, yukarıdaki sınıf sayesinde url ile veriyi çekebilirsin.

-- Bu sınıfı kullanarak veriyi çekmek için da asıl main  fonksiyonunda şunları tanımlamane gerekiyor.

 

package com.example.britishtower;

import org.json.JSONObject;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Uye_girisi extends ActionBarActivity {

	JSonParseSinifi benimnesnem=new JSonParseSinifi();
	SharedPreferences oturum;
	EditText txttc, txtsifre;
	Button btn;
	String tc, sifre;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_uye_girisi);
		
		//widgetlar tanımlanıyor.
		txttc=(EditText) findViewById(R.id.txttc);
		txtsifre=(EditText) findViewById(R.id.txtsifre);
		btn=(Button) findViewById(R.id.btngiris);
		txttc.requestFocus();

		//buton click eventı
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				tc=txttc.getText().toString();
				sifre=txtsifre.getText().toString();
				new kontrol().execute((Void) null);
			}
		});
	}

	String durum;
	int basari=0;
	//verileri veritabanından çekip kontrol etme sınıfı
	public class kontrol extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			JSONObject kontrol=benimnesnem.JSonObjeAl("http://192.168.5.28/android_connect/index.php/ogr_giris_kontrol?tc="+tc+"&sifre='"+sifre+"'");
			try {
//Benim json verimden "success bundle'ı geliyor, seninkinden hangisi geliyorsa onu çekebilirsin.
				if(kontrol.getInt("success")==0){
					durum="Şifre ya da TC No hatalı..";
					basari=0;
				}
				else{
					basari=1;
					durum="Başarılı bir şekilde giriş yapıldı.";
					oturum=getSharedPreferences("tc", 0);
                	SharedPreferences.Editor editor=oturum.edit();
                	editor.putString("tc", tc);
                	editor.commit();
				}
			}catch (Exception e) {
				durum="catch";
				// TODO: handle exception
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			if(basari==1){
				Intent intent=new Intent(getApplicationContext(), Anasayfa.class);
				startActivity(intent);
			}
			else{
				Toast.makeText(getApplicationContext(), durum, Toast.LENGTH_LONG).show();
			}
		}
		
	}
}