在应用中android需要调用远程WCF,参数数据传给了WCF,也返回了结果数据,然后郁闷的事发生了:利用返回的数据构建JSONObject总是报错,提示“WARN/System.err(489): org.json.JSONException: Value {"name":"Leon","sex":"male"} of type java.lang.String cannot be converted to JSONObject“,可我在debug中把{"name":"Leon","sex":"male"} 直接用于JSONObject是不报错的(new JSONObject({"name":"Leon","sex":"male"} );),求高人相助。部分代码如下:
                        jsonObject.put("usercode", "test");
                        jsonObject.put("password", "test");
                        
                        ByteArrayEntity se = new ByteArrayEntity(jsonObject.toString().getBytes("UTF8"));   
                        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
                        httppost.setEntity(se); 
                        
                        response = httpclient.execute(httppost);
                        if(response != null){                                                        
                            //InputStream in = response.getEntity().getContent();
                            String t = new String(EntityUtils.toString(response.getEntity()).getBytes("UTF8"));                                
                           //String t = convertStreamToString(in);
                           //String tt = t;
                           JSONArray json = new JSONArray(t);        (错误产生的地方) 

解决方案 »

  1.   

    汗 如果我没猜错的话 LZ你 JSONObject({"name":"Leon","sex":"male"} );)这里头的值是什么?{"name":"Leon","sex":"male"} 这样的结构,这个结构不是string
    但你从网络上获取下来的却是"  {"name":"Leon","sex":"male"}   " 这样的一个string
    你那代码相当于执行: JSONObject("{"name":"Leon","sex":"male"} ") 
      

  2.   

    2楼的兄弟,那是个笔误,应该是:new JSONObject("{"name":"Leon","sex":"male"}" );
      

  3.   

    请教楼主,android是怎么调用wcf服务啊?找了好久都没找到方法。
      

  4.   

    错误已经说的很明显了啊:不能把String类型作为JSONArray构造函数的参数。
    应该不string类型值先放入JSONObject,再放入JSONArray
      

  5.   

    JSONObject t = new JSONObject({"name":"Leon","sex":"male"} )
    JSONArray json = new JSONArray(t); 
    这样试试
      

  6.   

    JSONArray也支持String的构造函数
      

  7.   

    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
    String result= EntityUtils.toString(response.getEntity());
    try {
    JSONObject json=new JSONObject(result);
    JSONArray array=new JSONArray(result);
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return result;
    }