2017-08-17 56 views
0

我針對相同問題檢查了許多解決方案,但未解決任何問題。這就是爲什麼我再次發佈。 當我使用POSTMAN其正常工作。未找到Java類型,類org.json.JSONObject和MIME媒體類型application/json的消息正文編寫器

我試過沒有編碼。然後使用

「application/json; charset = utf-8」 沒有任何解決方案,我得到了。

我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" 
} 
} 

我不知道,我丟失的東西進去。任何投入都歡迎。謝謝。

+0

什麼不工作?你是否收到錯誤?請提供更多信息。 –

+0

我在pom.xml中全部更改爲相同的版本號爲1.19 \t \t \t com.sun.jersey \t \t \t 球衣束 \t \t \t 1.19 \t \t。我遇到了傳遞我的文本文件內容JSON類型的問題。請檢查MyJSON.txt文件。如果我沒有在post(ClientResponse.class)中傳遞此文件;那麼它的迴應如下:Status:[{「status」:「ERROR」,「message」:「JSON解碼錯誤」}]。 – Neel

+0

你的JSON對象看起來不錯。你有權訪問服務器日誌嗎?請張貼您在發佈JSON的服務器上看到的EXACT錯誤。 –

回答

0
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; 
     } 
相關問題