1

So I've been trying for a while to make many calls to an API with POST request, where I need to parse the access token of my devices in the URL, which I've stored them in an array,

String [] arr = new String[49];
        arr[0] = "Access_Token1";
        arr[1] = "Access_Token2"; 

What I need to do in to iterate through the array and get every access token, put it in the url to make the request, this is the code I used to make one call to the API, but I had no idea how to do for many post request

              String paramValue = "param\\with\\backslash";
            String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8");

            URL url2 = new URL("https://demo.thingsboard.io/api/v1/H2arM3bxFwobsGMAM5Rt/telemetry");
           for (int i=0; i<list.size(); i++) {
            HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Bearer");

            conn.setDoOutput(true);
            OutputStream outStream = conn.getOutputStream();
            
            OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
            
            outStreamWriter.write(list.get(1).toString());
            //outStreamWriter.write(list.toJSONString());
            outStreamWriter.flush();
            outStreamWriter.close();
            outStream.close();
            String response = null;

            System.out.println(conn.getResponseCode());
            System.out.println(conn.getResponseMessage());

            DataInputStream input1 = null;
            input1 = new DataInputStream (conn.getInputStream());
            while (null != ((response = input1.readLine()))) {
                System.out.println(response);
                input1.close ();
            }
        }}
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    

can anyone give me a clue how to do so, It's been 3 weeks trying to do that and I'm still stuck in here.

Paupiette
  • 47
  • 8
  • what is this object? `list.toJSONString()` – Phill Alexakis Jun 23 '20 at 12:53
  • I don't understand the problem you are having. You have a list of access tokens, and you know how to send a request? What is missing? Can you explain please? – Tanimak Jun 23 '20 at 13:48
  • @PhillAlexakis it is a list of json array which I want to send – Paupiette Jun 24 '20 at 09:25
  • @Tanimak As I've mentionned I could send one request which contains a list of JSONN objects, but now I'd like to send multiple requests to multiples devices which requires their access token so I'd to store them in an array so I can get each AT and replace it in the request, the problem when I've tried to make multiple connections it didn't work, so I don't know what is the simple way to modify my code. – Paupiette Jun 24 '20 at 09:28
  • we need the json payload that the endpoint expects – Phill Alexakis Jun 24 '20 at 10:09
  • @PhillAlexakis I have big json data which is saved in json file so what I've done is read the json file and pass as request , which I put in "list", this is an example of the content of my data [{"Latitude":"55.54832","Longitude":"14.51633","ts":1590347866000,"},...] – Paupiette Jun 24 '20 at 10:48
  • Please post the code you used to send multiple requests. Maybe we can spot the problem. However, what about something like `for (int i=0; i – lupz Jun 24 '20 at 11:32
  • Thank you for the reply, I've just edited my post may you have a look please. – Paupiette Jun 24 '20 at 12:51
  • 1
    @Paupiette great. Are you looking for `new URL("https://demo.thingsboard.io/api/v1/" + accessToken + "/telemetry");`? Do this inside the for loop to create a new Url (and thus connection etc) for each accessToken – lupz Jun 25 '20 at 09:28

0 Answers0