java.lang.String中无法转换为jsonobeject转换为、lang、java、jsonobeject

2023-09-03 22:59:14 作者:合衬酒友

我的问题是,当我试图从我的WCF服务解析到安卓它给了我一个错误。该回报我的服务是使用JSON。这是我的Andr​​oid code:

  DefaultHttpClient客户端=新DefaultHttpClient();
    // HTTP GET请求
    HTTPGET请求=新HttpGet("http://XXX.XXX.XXX.XXX:8181/Managers/Authentification.svc/authen/?loga="+t1.getText().toString()+"&pass="+t2.getText().toString());

    //设置hedear取得JSON甲酸数据
    request.setHeader(接受,应用/ JSON);
    request.setHeader(内容型,应用/ JSON);

    //得到响应
    HTT presponse响应= client.execute(要求);
    的System.out.println(Connectee --->中+ response.getAllHeaders());

    HttpEntity实体= response.getEntity();

    //如果实体contect len​​ght 0,是指在系统中使用这些code不存在数据
    如果(entity.getContentLength()!= 0){
        //流的读者对象
        读者employeeReader =新的InputStreamReader(response.getEntity()的getContent());
        //创建一个缓冲区,从读者填写
        的char []缓冲区=新的char [(INT)response.getEntity()getContentLength()];
        //填充缓冲区由读取器的帮助下
        employeeReader.read(缓冲液);
        //关闭读取流
        employeeReader.close();

        //为员工JSON对象
        JSONObject的TT =新的JSONObject(新的字符串(缓冲));

        的System.out.println(.................. OK ........................ 。);

        的System.out.println(参考:+ tt.getString(参考));



    }
    其他 {
        的System.out.println(.....................不正常...................... ..);
    }

}
赶上(JSONException E){
    的System.out.println(................... ERRORJSON ....................... 。);
    e.printStackTrace();
}赶上(例外五){
    // TODO自动生成的catch块
    的System.out.println(...................错误....................... 。);
    e.printStackTrace();
}

}
});
 

错误信息:

  05-06 16:34:04.256:我/的System.out(7257):Connectee ---> [Lorg.apache.http.Header; @ 40d2b900
05-06 16:34:04.296:我/的System.out(7257):................... ERRORJSON ............ ............
05-06 16:34:04.316:W / System.err的(7257):org.json.JSONException:值{参考:} java.lang.String类型不能转换成JSONArray
 

解决方案

u有一个数组属性:参考文献,它在JSONString被定义如下:     {参考:[]} 例如 {参考:[{ID:1,引用名称:名1},{ID:2,引用名称:名2}]}

同望造价软件实操 这三大问题你都遇到过吗

my problem is that when i try to parse from my wcf service into android it gives me an error. the return for my service is with JSON. this is my android code:

  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();
}

}
});

error message :

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

解决方案

u have an array attribute : Reference, which has to be defined in JSONString like this: {"Reference":[]} for example {"Reference":[{"id":1,"referenceName":"name1"},{"id":2,"referenceName":"name2"}]}