6 Haziran 2016 Pazartesi

Obje çekme

package com.example.mcp.getjsonobjectarray;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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


public class MainActivity extends AppCompatActivity {

TextView tv1, tv2, tv3, tv4, tv5, tv6;

RequestQueue requestQueue;
public static final String showUrl = "http://www.spoileral.com/layout/php/android/show.php";

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

tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
tv6 = (TextView) findViewById(R.id.textView6);

veriCek();



//
// JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
// showUrl, new Response.Listener<JSONObject>() {
// @Override
// public void onResponse(JSONObject response) {
// try {
//
// JSONArray sqlsorgu = response.getJSONArray("goster");
//
//// for(int i=0; i < sqlsorgu.length(); i++){
// JSONObject obje = sqlsorgu.getJSONObject(0);
// String id = obje.getString("id");
// String veriler = obje.getString("veriler");
//
// tv1.setText("ID: " + id.toString());
// tv2.setText("veri: " + veriler);
//
// String IDveri = id + " - " + veriler;
// tv3.setText(IDveri);
//
// //tv2.append("===\n");
//
//// }
//
//
// } catch (JSONException e) {
// e.printStackTrace();
// Toast.makeText(getApplicationContext(),"Veri çekme başarısız...",Toast.LENGTH_LONG).show();
// }
// }
// }, new Response.ErrorListener() {
// @Override
// public void onErrorResponse(VolleyError error) {
// Toast.makeText(getApplicationContext(),"Sorgu başarısız...",Toast.LENGTH_LONG).show();
// }
// });
// requestQueue = Volley.newRequestQueue(this);
// requestQueue.add(jsonObjectRequest);
//

}



private void veriCek() {

JsonObjectRequest veriCekRequest = new JsonObjectRequest(Request.Method.GET,
showUrl, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
//SERVERDAN SUCCSES GELIRSE
try {

JSONArray cekilenArray = response.getJSONArray("goster");

for(int i=0; i < cekilenArray.length(); i++){
JSONObject isimler=cekilenArray.getJSONObject(i);
String id=isimler.getString("id");
String veriler=isimler.getString("veriler");

tv1.setText("ID: " + id.toString());
tv2.setText("veri: " + veriler);

String dersKonu = id + " - " + veriler;
tv3.setText(dersKonu);


}

} catch (JSONException e) {
Toast.makeText(getApplicationContext(),"Bağlantı başarısız...",Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Sorgu Başarısız...",Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(veriCekRequest);

}



}

Hiç yorum yok:

Yorum Gönder