2015-10-24 34 views
0

我修改我的代碼使用Apache HttpComponents我被告知後,這是一個更簡潔的方法HttpURLConnection的修改Apache的HttpComponents

HttpURLConnection的代碼(工作):

String names = "names[]=EndUser/WebTransaction/WebTransaction/JSP/index.jsp"; 

try (PrintWriter writer = response.getWriter()) { 

      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); 

      conn.setRequestProperty("Accept", "application/json"); 
      conn.setRequestProperty("X-Api-Key", "myId"); 
      conn.setRequestMethod("GET"); 
      conn.setDoOutput(true); 
      conn.setDoInput(true); 

      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(names); 
      wr.flush(); 

      String line; 
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((line = reader.readLine()) != null) { 
       System.out.println(line); 
       writer.println(HTML_START + "<h2> NewRelic JSON Response:</h2><h3>" + line + "</h3>" + HTML_END); 
      } 
      wr.close(); 
      reader.close(); 
     }catch(MalformedURLException e){ 

      e.printStackTrace(); 
     } 

這是我的代碼修改爲使用阿帕奇HttpComponents(404未找到響應):

try (PrintWriter writer = response.getWriter()) { 
      HttpClient client = HttpClientBuilder.create().build(); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("X-Api-Key", "myID")); 
      nameValuePairs.add(new BasicNameValuePair("names[]", "EndUser/WebTransaction/WebTransaction/JSP/index.jsp")); 
      HttpGet request1 = new HttpGet(url + URLEncodedUtils.format(nameValuePairs, "utf-8")); 
      request1.setHeader("Accept", "application/json"); 


      HttpResponse response1 = client.execute(request1); 
      System.out.println(response1.getStatusLine().getStatusCode()); 


      String line; 
      BufferedReader reader = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); 
      while ((line = reader.readLine()) != null) { 
       System.out.println(line); 
       writer.println(HTML_START + "<h2> NewRelic JSON Response:</h2><h3>" + line + "</h3>" + HTML_END); 
      } 

      reader.close(); 
     }catch(MalformedURLException e){ 

      e.printStackTrace(); 
     } 

有人可以指出正確的方式來完成此請。

+0

當問一個問題,包括所有必要的信息。 「不工作」對任何參與者都沒有幫助。 – Kayaman

+0

對不起,我收到了一個未找到的404響應 – Johntk

+2

您可以使用wireshark或類似的工具來比較發送的請求是否不同。 – Kayaman

回答

0

更簡潔的方法是使用像庫一樣的Retrofit,因爲這些都是樣板代碼。

您仍然可以使用此代碼作爲一種通用方法來引入Json對象,以便您可以處理它們並從中獲得所需的任何必要信息。但它不是更清潔,它相信我。 :)

由於我沒有你的實際API網址,我會嘗試使用this API function舉個例子。

改裝是類型安全的,這意味着你指定了模型pojo,它會照顧到對你的模型本身很酷的Json對象進行必要的轉換。

模型,

public class Application { 

    private Integer id; 
    private String name; 
    private String language; 
    private String health_status; 

    //Getters and setters 

} 

DTO,

public class ApplicationListDot { 

    private List<Application> applications; 

} 

接口,

public interface RestController { 

    @GET("/v2/applications.json") 
    ApplicationListDot viewApplications(); 

} 
+0

這不是一個答案,這是一條評論。 – Kayaman

+0

我可以讓這個答案 –

+1

然後這樣做。建議另一個圖書館不是答案。 – Kayaman

相關問題