我用 JSON 创建了一个 web 服务,我想解析它然后把它们放在 listview 上。但是当我运行应用程序时,它会强制关闭并显示错误消息。请检查我的代码,有什么问题吗?
这是我尝试运行我的应用程序时的错误消息:
org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
这是我的代码:
public class WeeklyFlashActivity 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.greenfields.co.id:502/Service1.svc/json/weeklyflash");
        try{
            JSONArray  weeklyflash = json.getJSONArray("GetReportResult");
            for(int i=0;i<weeklyflash.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = weeklyflash.getJSONObject(i);
                //map.put("type",  String.valueOf(i));
                map.put("type", e.getString("type"));
                map.put("total", e.getString("total"));
                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[] { "type", "total" }, 
                        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(WeeklyFlashActivity.this, "Type " + o.get("type") + " was clicked.", Toast.LENGTH_SHORT).show(); 
            }
        });
    }
}
这是我的 JSON 变量:
{"GetReportResult":[{"total":"249319","type":"ESL500ML"},{"total":"19802","type":"CHEESE1K"},{"total":"3179380","type":"ESL"},{"total":"201243","type":"WHP"},{"total":"736744.136","type":"UHT"}]}
这是我的 JSON 函数:
public class JSONfunctions {
    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,"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 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;
    }
}
更新:我检查了我的结果,结果是一个 html 代码,并说Method not allowed.问题可能来自我的 wcf 服务。你有什么解决办法吗?
这是我的 wcf 服务:
[WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/json/weeklyflash")]