Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法将Java.lang.String转换为jsonobeject_Android_Json_Wcf - Fatal编程技术网

Android 无法将Java.lang.String转换为jsonobeject

Android 无法将Java.lang.String转换为jsonobeject,android,json,wcf,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

我的问题是,当我试图从我的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

你有一个数组属性:Reference,它必须在JSONString中定义如下: {“参考”:[]} 例如
{“Reference”:[{“id”:1,“referenceName”:“name1”},{“id”:2,“referenceName”:“name2”}]}

在尝试创建JSONObject并将其发布到此处之前,记录服务器的响应。显然,您没有得到有效的JSON。请不要发布问题两次!参考“:”Objet“{”登录“:null”,Mtspass“:null,“代码会话“:”616e0e76-fc5e-4df7-9079-d65cc15f1dd7“}”,状态代码“:”状态标签“:”}不,这不是来自服务器的整个响应看起来像:{参考“:”Objet“{”登录“:null”,Mtspass“:null,“代码会话“:”616e0e76-fc5e‌​-4df7-9079-d65cc15f1dd7“},“状态代码”:“状态标签”:“}做一个System.out.println((新字符串(缓冲区)),让我看看结果05-07 15:01:51.002:I/System.out(8550):“{”引用\“:”测试\“,\”对象\“:{”登录\“:null,\“Mtspass\”:null,\“代码会话\:\”:“6916cd6a-6a0e-4ee4-a162-E24E672F\”,“状态代码”:“状态标签”:”这是一个问题,通常情况下,它必须像{“引用”:“test”,…}它不应该有反斜杠,我不知道它来自哪里,但尝试验证您的web服务我发现了问题,它是反斜杠并引用这是我的解决方案:String x=新字符串(缓冲区);String y=x.replace(\\”,“”);if(y.startsWith(“\”){y=y.substring(1,y.length());}if(y.endsWith(“\”){y=y.substring(0,y.length()-1);}JSONObject reader=new JSONObject(y);