Java.lang.String无法转换为jsonobject

时间:2014-05-06 17:20:58

标签: android json wcf

我的问题是,当我尝试从我的wcf服务解析到android时,它给了我一个错误。我的服务的回报是使用JSON。这是我的android代码:

  DefaultHttpClient client = new DefaultHttpClient();
    // http get request
    HttpGet request = new HttpGet("http://XXX.XXX.XXX.XXX:8181/Managers/Authentification.svc/authen/?loga="+t1.getText().toString()+"&pass="+t2.getText().toString());

    // set the hedear to get the data in JSON formate
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");

    //get the response
    HttpResponse response = client.execute(request);
    System.out.println("Connectee--->" +response.getAllHeaders());

    HttpEntity entity = response.getEntity();

    //if entity contect lenght 0, means no data exist in the system with these code
    if(entity.getContentLength() != 0) {
        // stream reader object
        Reader employeeReader = new InputStreamReader(response.getEntity().getContent());
        //create a buffer to fill it from reader
        char[] buffer = new char[(int) response.getEntity().getContentLength()];
        //fill the buffer by the help of reader
        employeeReader.read(buffer);
        //close the reader streams
        employeeReader.close();

        //for the employee json object
        JSONObject tt =  new JSONObject(new String(buffer));

        System.out.println("..................OK.........................");

        System.out.println("Reference: " + tt.getString("Reference"));



    }
    else {
        System.out.println("...................Not OK........................");
    }

}
catch (JSONException e) {
    System.out.println("...................ERRORJSON........................");
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("...................ERROR........................");
    e.printStackTrace();
}

}
});

错误消息:

05-06 16:34:04.256: I/System.out(7257): Connectee--->[Lorg.apache.http.Header;@40d2b900
05-06 16:34:04.296: I/System.out(7257): ...................ERRORJSON........................
05-06 16:34:04.316: W/System.err(7257): org.json.JSONException: Value {"Reference":""} of type java.lang.String cannot be converted to JSONArray

1 个答案:

答案 0 :(得分:2)

你有一个数组属性:Reference,必须在JSONString中定义,如下所示:     {"参考":[]} 例如 {"参考":[{" ID":1,"引用名称":" NAME1"},{" ID&#34 ;:2"引用名称":" NAME2"}]}