通过Java执行http客户端时出现JSONObject文本格式错误
我有这样的代码,我正在执行一个通过Java执行http客户端时出现JSONObject文本格式错误,java,http,httpclient,Java,Http,Httpclient,我有这样的代码,我正在执行一个http客户机,以获得响应 public String exmp(StringBuilder sql, String table){ authCache = new BasicAuthCache(); basicAuth = new BasicScheme(); authCache.put(target, basicAuth); localContext = HttpClientContext.create(); local
http客户机
,以获得响应
public String exmp(StringBuilder sql, String table){
authCache = new BasicAuthCache();
basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
String postJsonstr=null;
try {
HttpPost httpPost = new HttpPost(API_URL);
StringBuilder str = new StringBuilder();
str.append("{");
str.append("\"format\":\"csv\",");
str.append("\"version\":\"1.0\",");
str.append("\"name\":\""+table+"\",");
str.append("\"encrypted\":\"none\",");
str.append("\"queries\":[{");
str.append("\"name\":\""+table+"\",");
str.append("\"query\":\"" + sql + "\",");
str.append("\"type\":\"export\"");
str.append("}]");
str.append("}");
httpPost.addHeader("accept", jsonContentType);
StringEntity posEnt = new StringEntity(str.toString().trim());
posEnt.setContentType(jsonContentType);
httpPost.setEntity(posEnt);
CloseableHttpResponse response = httpclient.execute(target, httpPost, localContext);}
当我执行这段代码时,我得到的错误是:org.json.JSONException:JSONObject文本必须以“{”开头,位于1[字符2第1行]
我试着做System.out.println((int)sb.toString().trim().charAt(0));
来看看第一个字符是什么,它是123
,根据一些帖子,它是“{”的正确代码
在上面的代码中,httpclient
是类(static CloseableHttpClient-httpclient;
)的全局变量,并在一个函数中初始化,该函数在上述函数之前从main
使用httpclient=HttpClients.custom().setDefaultCredentialsProvider(凭证).build();
调用
当我在第一个字符处有{
时,我无法理解为什么会出现此错误
更新的代码:这是我使用JSONObject
更新的代码。我仍然收到相同的错误
public String exmp(StringBuilder sql, String table){
authCache = new BasicAuthCache();
basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
String postJsonstr=null;
try {
HttpPost httpPost = new HttpPost(API_URL);
JSONObject sbjson = new JSONObject();
sbjson.put("format", "csv");
sbjson.put("version", "1.0");
sbjson.put("name", table);
sbjson.put("encrypted", "none");
JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("name", table);
item.put("query", sql);
item.put("type", "export");
array.put(item);
sbjson.put("queries", array);
httpPost.addHeader("accept", jsonContentType);
StringEntity postEntity = new StringEntity(sbjson.toString());
posEnt.setContentType(jsonContentType);
httpPost.setEntity(posEnt);
CloseableHttpResponse response = httpclient.execute(target, httpPost, localContext);}
你为什么不使用JSON API来创建JSON,而不是手工构建它?@JonSkeet我如何通过JSON API创建上述结构的JSON?你能给我举个例子吗?你需要创建一个JSONObject(或任何你使用的API中的等价物),然后将
format
属性设置为“csv”
,将版本
属性设置为“1.0”
等。您尝试了什么吗?@JonSkeet如果我创建了一个JSONObject,那么如何在httpPost.setEntity(posEnt)中设置实体;
它需要一个StringEntity
对象和StringEntity
不接受JSONObject类型,只接受字符串类型。而且,不,我没有尝试过JSON API,这就是为什么我问你能不能给我一个例子,所以把JSONObject转换成字符串…重点是这样你就不需要担心处理所有的JSON API了在寻求帮助之前,你应该自己尝试一下JSON API——能够研究不同的技术是很重要的,即使有时你会被卡住。(提示:我强烈怀疑你可以找到一些使用HttpClient和JSON API将JSON发布到web服务的示例…)我的代码中的StringBuilder
与您发布的String.format()
version有什么区别?我也尝试了JSONObject
version(文章更新部分的代码)但我还是犯了同样的错误。你可以获取JSON字符串并检查问题出在哪里。我可以在线建议格式化和验证。
String.format("{\"format \":\"csv \""
+ ",\"version \":\"1.0 \""
+ ",\"name \":\"%s\""
+ ",\"encrypted \":\"none \""
+ ",\"queries \":[{\"name \":\"%s\"\""
+ ",\"query\":\"%s\""
+ ",\"type\":\"export\"}]}",
table, table, sql);