简体   繁体   中英

HTTP POST request as a array of data

I have a method that retrieves info from a online API however i'm unsure on how to retrieve this information in some constructive way like in an array. Here is my code that i have:

private void sendPost() throws Exception {

    String url = "http://www.imei.info/api/checkimei/";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();


    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "imei=00000000000000";


    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();




    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;


    while ((inputLine = in.readLine()) != null) {
        System.out.println("Line: "+inputLine);
    }
    in.close();



}

It prints a string but the string isn't in any constructive form:

Testing 2 - Send Http POST request
Line: {"info": {"weight": 130.0, "battery": ["Li-Ion", 2600.0], "qwerty": false, "year":

... and so on

I've removed my imei testing info from the code.

I am not sure if what you want is only save many strings in order to use after, if this is the case why not create an array of string and put into in a variable?

    List<String> result= new ArrayList<>();
 while ((inputLine = in.readLine()) != null) {
        result.add("Line: "+inputLine);
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM