【发布时间】:2018-01-26 03:14:50
【问题描述】:
我针对同一个问题检查了许多解决方案,但没有解决任何问题。这就是我再次发帖的原因。 当我使用 POSTMAN 时,它工作正常。
我也试过不编码。然后用了
“应用程序/json;字符集=utf-8” 我没有任何解决方案。
我的客户代码:
public static void main(String[] args) throws JSONException, IOException
{
Test1 my_client = new Test1();
File file_upload = new File("C:/MyJSON.txt");
my_client.sendFileJSON(file_upload);
}
private void sendFileJSON(File file_upload) throws JSONException, IOException{
ClientConfig config = new DefaultClientConfig();
// config.getClass().add(MOXyJsonProvider.class);
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");
JSONObject data_file = new JSONObject();
data_file.put("file_name", file_upload.getName());
data_file.put("file", convertFileToString(file_upload));
ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).header("Authorization", "Basic Y3lDi543XJzcEFsMkJ1Fm0xV2Ic5").post(ClientResponse.class, data_file);
System.out.println("Status: "+client_response.getEntity(String.class));
client.destroy();
}
//Convert my file to a Base64 String
private String convertFileToString(File file) throws IOException{
byte[] bytes = Files.readAllBytes(file.toPath());
return new String(Base64.encode(bytes));
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DataAnalytics</groupId>
<artifactId>DataAnalytics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm-all</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<!-- <dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.2</version>
</dependency> -->
</dependencies>
</project>
MyJSON.txt
{
"header":{
"user":"XXXX",
"password": "XXXXXXXXXXXXXX",
"table":"licf",
"method_override":"get"
}
}
我不知道,我错过了一些东西。欢迎任何意见。谢谢。
【问题讨论】:
-
什么不起作用?您是否收到错误消息?请提供更多信息。
-
我在 POM.xml 中更改为所有相同的版本号为 1.19
。我在传递内容 JSON 类型的文本文件时遇到问题。请检查 MyJSON.txt 文件。如果我没有在 post(ClientResponse.class) 中传递这个文件;然后它给我这样的回应:状态:[{“状态”:“错误”,“消息”:“JSON解码错误”}]。com.sun.jersey jersey-bundle 1.19 -
您的 JSON 对象看起来不错。您有权访问服务器日志吗?请发布您要向其发布 JSON 的服务器上看到的 EXACT 错误。
-
@LukasBradley No. URL,我正在使用该第 3 方 URL。我现在正在尝试使用 JACKSON 创建 JSON 对象。我不确定这是 JSONobject 问题还是什么?你能帮我把我的JSON变成字符串吗?我试过但显示编译错误。字符串输入= "{\"header\":"{\"user\":"XXX",\"password\":\"af965a6fcb025b3740cd2dd9af\",\"table\":\"licf\",\"method_override \":\"get\"}}"; 所以我可以通过将此输入直接传递到 post(ClientResponse.class, input) 来检查
-
得到了结果。通过使用杰克逊创建 JSON 并传递它。非常感谢您的意见。
标签: java json web-services maven pom.xml