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 对象的通用方法,以便您可以处理它们并从 it.But 中获取您想要的任何必要信息它并不干净,相信我,这很乱。 :)

因为我没有你的实际 API url 我会尝试使用 this API function.

来举个例子

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();

}