【发布时间】:2016-04-27 16:07:54
【问题描述】:
程序正在从 web 获取 json 并添加到 ListView 但我有错误 我的代码:
Thread thread =new Thread()
{
@Override
public void run() {
Looper.prepare();//En başta gerekli
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),5000);
try {
HttpPost post = new HttpPost("http://"+MainActivity.baglanti_adres+"/not/notlar_oku.php");
List<NameValuePair> gonderilenler = new ArrayList<NameValuePair>(2);//Verileri göndermek için arraylist tanımlama
gonderilenler.add(new BasicNameValuePair("eposta",kullanici_eposta));
post.setEntity(new UrlEncodedFormEntity(gonderilenler,"UTF_8"));//post'a verileri UTF-8 olarak ekleme
HttpResponse response = client.execute(post);//Verileri gönderme
if(response!=null)//Sayfadan gelen veri boş değil ise
{
BufferedReader oku = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));//Sayfadan gelen veriyi okumak oluşturma
StringBuilder kodlar =new StringBuilder();//Kodların eklendiği string
String satir;//döngüde kullanmak için string
while ((satir = oku.readLine())!=null)//döngü ile sayfadaki kodları alma
{
kodlar.append(satir+"\n");//Satir satır kodları ekleme
}
oku.close();
ArrayList<String> notlar_dizisi = new ArrayList<String>();//Gelen notları eklemek için arraylist
JSONArray json_dizi = new JSONArray(kodlar.toString());//kodları json dizisi yapma
if(json_dizi.length()>0)//json dizisinin boyutunu kontrol etme
{
for (int i =0;i<json_dizi.length();i++)
{
notlar_dizisi.add(json_dizi.getJSONObject(i).getString("not")+ " - "+json_dizi.getJSONObject(i).getString("Tarih")+ " - "+json_dizi.getJSONObject(i).getString("ID"));//Kullanıcıya ait notları arraliste alma
}
}
ArrayAdapter<String> notlar_adapter =new ArrayAdapter<String>(Anasayfa.this,android.R.layout.simple_list_item_1,android.R.id.text1,notlar_dizisi);//Arraylistteki notları listview e atmak için array adapter oluşturma
notlar.setAdapter(notlar_adapter);//Notları listviewe ekleme
}
}
catch (Exception e)
{
Log.e("Hata1!",e.getMessage());
}
Looper.loop();
}
};
thread.start();//Veri göndermek için thread oluşturma ve başlatma
【问题讨论】:
-
请格式化您的问题并提供更多信息并编码您尝试做某事的内容/时间/地点
-
克里斯蒂安,你能帮帮我吗?我是新手,我不知道该怎么做。
-
简单回答:使用 AsyncTask 和 Volley 库。