我是android编程的新手,我有一个问题。我从thislink Json数据获取并将这些数据加载到我的自定义列表视图中,但是在加载时我崩溃了。 错误是这样的:解析结果错误,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)
答案 0 :(得分:1)
您的第一个错误 android.os.NetworkOnMainThreadException 即将发布,因为您在主线程中解析了Json。尝试使用Asnync Class。如果您不想使用Async类而不是在onCreate中粘贴此代码。
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
答案 1 :(得分:0)
首先使用BackGround中的AsyncTask调用Json From URL检查this链接,你正在做的是从主线程调用这是错误的,这就是为什么你得到 NETWORK ON MAIN THREAD EXCEPTION 我想这是导致应用崩溃的主要原因。