我有这个代码,我正在执行http client
以获得回复。
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: A JSONObject text must begin with '{' at 1 [character 2 line 1]
。
我尝试System.out.println((int)sb.toString().trim().charAt(0));
来查看第一个字符的来源,123
根据某些帖子是“{”的正确代码。
在上面的代码中,httpclient
是类(static CloseableHttpClient httpclient;
)的全局变量,并在一个函数中初始化,该函数在使用httpclient = {{main
的上述函数之前调用1}}。
当我在第一个字符HttpClients.custom().setDefaultCredentialsProvider(credentials).build();
时,我无法理解为什么会出现此错误。
更新后的代码:这是我使用{
的更新代码。我仍然得到同样的错误
JSONObject
答案 0 :(得分:0)
String.format("{\"format \":\"csv \""
+ ",\"version \":\"1.0 \""
+ ",\"name \":\"%s\""
+ ",\"encrypted \":\"none \""
+ ",\"queries \":[{\"name \":\"%s\"\""
+ ",\"query\":\"%s\""
+ ",\"type\":\"export\"}]}",
table, table, sql);