当前位置: 首页 > 知识库问答 >
问题:

Redmine Java api-org.json.jsonException:JSONObject文本必须以字符1处的“{”开头

申屠宗清
2023-03-14

当我尝试运行这段代码时(问题示例摘自该web http://www.redmine.org/projects/redmine/wiki/rest api with java):

    RedmineManager redmineManager = new RedmineManager("http://something.cz", "somekey"); //I changed these two parameters for security reasons when paste here
    System.out.println(redmineManager.getProjects().size()); //this works fine
    Issue issueToCreate = new Issue();
    issueToCreate.setSubject("This is the summary line 123");
    Issue newIssue = redmineManager.createIssue("test-project", issueToCreate); //this line throws exception
Exception in thread "main" com.taskadapter.redmineapi.RedmineFormatException: org.json.JSONException: A JSONObject text must begin with '{' at character 1
    at com.taskadapter.redmineapi.internal.Transport.parseResponse(Transport.java:456)
    at com.taskadapter.redmineapi.internal.Transport.addObject(Transport.java:186)
    at com.taskadapter.redmineapi.RedmineManager.createIssue(RedmineManager.java:135)
    at javaapplication146.JavaApplication146.main(JavaApplication146.java:27)
Caused by: org.json.JSONException: A JSONObject text must begin with '{' at character 1
    at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
    at org.json.JSONObject.<init>(JSONObject.java:179)
    at org.json.JSONObject.<init>(JSONObject.java:402)
    at com.taskadapter.redmineapi.internal.RedmineJSONParser.getResponseSingleObject(RedmineJSONParser.java:609)
    at com.taskadapter.redmineapi.internal.Transport.parseResponse(Transport.java:454)
    ... 3 more
Java Result: 1

我正在使用maven(http://mvnrepository.com/artifact/com.taskadapter/redmine-java-api),它依赖于1.23版,但是我尝试了1.12.0,也抛出了相同的异常。

我们使用的是最新的redmine 2.5.1。稳定的,但是基于stacktrace,redmine web和这个redmine api之间的通信不会发生,因为异常是在应用程序的“Parserespone”部分抛出的。

共有1个答案

海翔宇
2023-03-14

不幸的是,根据与库所有者的讨论,https://github.com/taskadapter/redmine-java-api/issues/126看起来这个问题仍然没有解决(或无法解决)

但是,GET请求工作。对于Update/Create,您可以使用纯API,如下所示。警告:禁用SSL是危险的,只能用于测试目的(比如“它能工作吗?”)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 *
 * @author libik
 */
public class JavaApplication147 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        disableCertificateValidation();        

        URL url = new URL("https://redmine.***.cz/issues.xml");
        HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();
        httpCon.setRequestProperty("X-Redmine-API-Key", "*****");
        httpCon.setRequestProperty("Content-Type", "application/xml");
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod("POST");
        OutputStreamWriter out = new OutputStreamWriter(
                httpCon.getOutputStream());
        out.write("<?xml version=\"1.0\"?>\n" +
"<issue>\n" +
"  <project_id>pan-unicorn-bot</project_id>\n" +
"  <subject>Created from netbeans by name without priority</subject>\n" +
"</issue>");
        out.close();

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            br = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(sb.toString());
    }

    public static void disableCertificateValidation() {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }};

        // Ignore differences between given hostname and certificate hostname
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
        } catch (Exception e) {
        }
    }

}
 类似资料: