Android JSON Parse
Haber sitesinden veri cekmek istiyorum. App calisdirdikda onPostExecute() methodundaki toast calisiyor. Hata nerde bulamiyorum
package com.example.xeberapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ActionBarActivity {
ArrayList<Xeberler> xeberlerList;
ListView listview;
XeberAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xeberlerList = new ArrayList<Xeberler>();
new JSONAsyncTask().execute("http://qafqazinfo.az/api.php");
listview = (ListView)findViewById(R.id.listView1);
adapter = new XeberAdapter(getApplicationContext(), R.layout.xeberler, xeberlerList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(), xeberlerList.get(position).getP_title(), Toast.LENGTH_LONG).show();
}
});
}
class JSONAsyncTask extends AsyncTask<String, String, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httpGet = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpGet);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("api_adi");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Xeberler xeber = new Xeberler();
xeber.setP_image(object.getString("image"));
xeber.setP_title(object.getString("title"));
xeber.setP_title_sonluq(object.getString("title_sonluq"));
xeber.setP_detail(object.getString("detail"));
xeberlerList.add(xeber);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false){
Toast.makeText(getApplicationContext(), "Serverden veri getirilemedi", Toast.LENGTH_LONG).show();
}
}
}
}
public class XeberAdapter extends ArrayAdapter<Xeberler> {
ArrayList<Xeberler> xeberlerList;
LayoutInflater vi;
int Resource;
//Context context;
ViewHolder holder;
public XeberAdapter(Context context, int resource, ArrayList<Xeberler> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
xeberlerList = objects;
//this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.xeberBasliq = (TextView) v.findViewById(R.id.titleBasliq);
holder.xeberSonluq = (TextView) v.findViewById(R.id.titleSonluq);
holder.xeberMelumat = (TextView) v.findViewById(R.id.detail);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imageview).execute(xeberlerList.get(position).getP_image());
holder.xeberBasliq.setText(xeberlerList.get(position).getP_title());
holder.xeberSonluq.setText(xeberlerList.get(position).getP_title_sonluq());
holder.xeberMelumat.setText(xeberlerList.get(position).getP_detail());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView xeberBasliq;
public TextView xeberSonluq;
public TextView xeberMelumat;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
System.out.println(e);
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
public class Xeberler {
private String p_image;
private String p_title;
private String p_title_sonluq;
private String p_detail;
public Xeberler(){
}
public Xeberler(String p_image, String p_title, String p_title_sonluq, String p_detail) {
super();
this.p_image = p_image;
this.p_title = p_title;
this.p_title_sonluq = p_title_sonluq;
this.p_detail = p_detail;
}
public String getP_title_sonluq() {
return p_title_sonluq;
}
public void setP_title_sonluq(String p_title_sonluq) {
this.p_title_sonluq = p_title_sonluq;
}
public String getP_image() {
return p_image;
}
public void setP_image(String p_image) {
this.p_image = p_image;
}
public String getP_title() {
return p_title;
}
public void setP_title(String p_title) {
this.p_title = p_title;
}
public String getP_detail() {
return p_detail;
}
public void setP_detail(String p_detail) {
this.p_detail = p_detail;
}
}