2011-07-30 60 views
0

下面是我的JSON解析应用程序的代码。只要我在我的模拟器上运行,它就会关闭。我是Android编程的新手,我自学成功,请耐心等待。谢谢!JSON解析应用程序“无数据”?

JSONfunctions.java:

package com.pxr.tutorial.json; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
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.entity.UrlEncodedFormEntity; 
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.util.Log; 

public class JSONfunctions { 

public static JSONObject getJSONfromURL(String url){ 
    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,"iso-8859-1"),8); 
      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{ 

     jArray = new JSONObject(result);    
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 
} 

Main.java:

package com.pxr.tutorial.json; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import com.pxr.tutorial.xmltest.R; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class Main extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listplaceholder); 

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


    JSONObject json = JSONfunctions.getJSONfromURL 
("http://www.tastekid.com/ask/ws?q=mosdef&f=musifin2125&k=mjjlnzkyzwuz&format=JSON");     
    try{ 

     JSONArray similar = json.getJSONArray("similar"); 

     for(int i=0;i<similar.length();i++){       
      HashMap<String, String> map = new HashMap<String, String>();  
      JSONObject e = similar.getJSONObject(i); 

      map.put("id", String.valueOf(i)); 
      map.put("name", "Name:" + e.getString("name")); 
      map.put("type", "Type: " + e.getString("type")); 
      mylist.add(map);    
     }  
    }catch(JSONException e)  { 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
        new String[] { "name", "type" }, 
        new int[] { R.id.item_title, R.id.item_subtitle }); 

    setListAdapter(adapter); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) {     
      @SuppressWarnings("unchecked") 
      HashMap<String, String> o = (HashMap<String, String>) 
lv.getItemAtPosition(position);     
      Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", 
Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
} 

编辑 - 固定未处理的异常,但现在它说: “没有数据” 当我试图解析URL。

再次感谢!

+0

的是什么logcat中显示添加许可标签? –

+0

我试图使用logcat,但提供了太多的数据,我不知道如何对它进行排序。你想要所有的数据吗? – Slicekick

+1

红色的东西(异常堆栈转储)。 –

回答

0

这里是你的错误:

JSONArray similar = json.getJSONArray("similar");

  1. 在解析JSON你必须要注意,这是区分大小写,我看到你的JSON对象是Similiar但你设法得到它为similiar
  2. Similiar是一个JSON对象,但你试图把它作为一个JSONArray

这里是你的代码修复,我给你一个修复得到InfoSimiliar对象:

 JSONObject earthquakes = json.getJSONObject("Similar"); 
     JSONArray info = earthquakes.getJSONArray("Info"); 

     for (int i = 0; i < info.length(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      JSONObject e = info.getJSONObject(i); 

      map.put("id", String.valueOf(i)); 
      map.put("name", "Name:" + e.getString("Name")); 
      map.put("type", "Type: " + e.getString("Type")); 
      mylist.add(map); 
     } 

用上面的代码在JSONObject json = JSONFun.getJSONfromURL("http://www.tastekid.com/ask/ws? q=mosdef&f=musifin2125&k=mjjlnzkyzwuz&format=JSON"); 之后替换try之内的代码块。

+0

我在JSONlint上检查过它,并验证了它。你在哪里看到我的代码中的parseResponse()部分?我无法在任何地方看到它。 – Slicekick

+0

呃?它在那里,当我以前试过,看起来像你改变了你的代码的网址?如果你不是那么好,那么我发现了一些错误,我会编辑我的答案。 – ayublin

+0

@Slicekick我已经编辑了我的答案,你可以现在检查它。 – ayublin

0

你在你的清单文件,该文件允许使用互联网

+0

是的,我已经做到了。 – Slicekick