gpt4 book ai didi

java - 加载json数据并将其绑定(bind)到自定义布局

转载 作者:行者123 更新时间:2023-12-01 13:25:52 26 4
gpt4 key购买 nike

我是 Android 编程新手,有一个问题。我从 thislink 获取信息Json 数据并将这些数据加载到我的自定义 ListView 中,但是加载时我崩溃了。错误是这样的:解析结果错误,以及http连接错误。

这是我的main.java,

package com.example.dovizkuru;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;



public class AnaEkran extends Activity{
public static final String URL = "http://api.piyasa.com/json/?kaynak=doviz_guncel_serb";

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle skr) {
// TODO Auto-generated method stub
super.onCreate(skr);
setContentView(R.layout.anaekran);

JSONObject json =getJSONfromURL(URL);

UyarıGoster("String mesaj");
//Loop the Array
try{
JSONArray kurlar = json.getJSONArray("");
for(int i=0;i < kurlar.length();i++){

HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = kurlar.getJSONObject(i);

map.put("foexid", String.valueOf(i));
map.put("foex", "Ad : " + e.getString("foex"));
map.put("buy", "Alış : " + e.getString("buy"));
map.put("sell", "Satış : " + e.getString("sell"));
mylist.add(map);
}
}
catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}


SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
mylist, R.layout.mymain,new String[] { "foex", "buy","sell" },
new int[] {R.id.foex,R.id.tvParaAlis,R.id.tvParaSatis });

ListView listView = ( ListView ) findViewById(R.id.listview);

// Setting the adapter to the listView
listView.setAdapter(adapter);

}



private void UyarıGoster(String mesaj){
Toast.makeText(getApplicationContext(),
mesaj, Toast.LENGTH_LONG)
.show();


}


public static String convertStreamToString(InputStream is){
//burada gelen veriyi string değerine çevireceğiz
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return sb.toString();
}









public static JSONObject getJSONfromURL(String url){

//initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

//try parse the string to a JSON object
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}

return jArray;
}


}

这里有错误数据

02-16 16:52:49.141: E/Trace(13341): error opening trace file: No such file or directory (2)
02-16 16:52:55.011: E/log_tag(13341): Error in http connection android.os.NetworkOnMainThreadException
02-16 16:52:55.011: E/log_tag(13341): Error converting result java.lang.NullPointerException
02-16 16:52:55.031: E/log_tag(13341): Error parsing data org.json.JSONException: End of input at character 0 of
02-16 16:52:55.401: W/dalvikvm(13341): threadid=1: thread exiting with uncaught exception (group=0x40f91438)

最佳答案

你的第一个错误android.os.NetworkOnMainThreadException出现了,因为你在主线程中解析你的Json。尝试使用 Asnync 类来实现这一点。如果您不想使用 Async 类,请将此代码粘贴到 onCreate 中。

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

关于java - 加载json数据并将其绑定(bind)到自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com