I checked many solutions for same question but didn't resolve any. Thats why I am posting again . When I used POSTMAN its working properly.
I tried with without encoding as well. Then used
"application/json; charset=utf-8" No any solution I got.
My ClientCode:
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"
}
}
I do not know, something I am missing into it. Any inputs are welcome. Thank you.
public static void main(String[] args) throws IOException {
Parent p = new Parent();
Header h = new Header();
h.setUser("XXX");
h.setPassword("af962d0ceacdd9af");
h.setTable("licf");
h.setMethod_override("get");
p.setHeader(h);
Test t = new Test();
String jsonInString = t.writeJSON(p);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");
ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).
header("Authorization", "Basic Y3liZXJzcGFI3UGc5").
post(ClientResponse.class,jsonInString);
System.out.println("Status: "+client_response.getEntity(String.class));
client.destroy();
}
private String writeJSON(Parent p) throws JsonGenerationException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(p);
return jsonInString;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.