簡體   English   中英

HttpURLConnection修改為Apache HttpComponents

[英]HttpURLConnection modified to Apache HttpComponents

我被告知這是一種更干凈的方法后,我正在修改我的代碼以使用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();
        }

這是我的代碼修改為使用Apache 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();
        }

有人可以向我指出完成此操作的正確方法。

更干凈的方法是像庫一樣使用Retrofit,因為這些是樣板代碼。

您仍然可以使用此代碼作為引入Json對象的通用方法,以便您可以處理它們並從中獲取所需的任何必要信息。但是它並不干凈,請相信我。 :)

由於我沒有您實際的API網址,因此我將嘗試使用此API函數給出一個示例。

Retrofit是類型安全的,這意味着您可以指定模型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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM