0
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

//this is my code. I'm trying to make an HTTP post request to Ubidots. How to send HTTP Post Request with a value in Java (Android Studio) to Ubidots?

class soll{
         protected void doInBackground(String... params) {
            String urlString = params[0]; // URL to call
            String data = params[1]; //data to post
            OutputStream out = null;

            try {
                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                urlConnection.setReadTimeout(10000);
                urlConnection.setConnectTimeout(15000);
                out = new BufferedOutputStream(urlConnection.getOutputStream());

                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                writer.write(data);
                writer.flush();
                writer.close();
                out.close();

                urlConnection.connect();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

public class CallAPII{
    public static void main(String[] args) {
        soll obj = new soll();
        obj.doInBackground("http://things.ubidots.com/api/v1.6/","{\"value\":\"1\"}");
    }
}

//this is my code and i'm not able to figure out the error

Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27
  • please add the error information – LhasaDad Sep 14 '19 at 17:41
  • Hey. I'm getting a blank screen on running the code in Java Eclipse and the value which I'm passing in my code to Ubidots is not getting changed on Ubidtots itself – Rahul Trivedi Sep 14 '19 at 17:44
  • Possible duplicate of [Java - sending HTTP parameters via POST method easily](https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) – swpalmer Sep 14 '19 at 20:28

1 Answers1

0

the code below should do the trick

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package httpexample;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jose garcia
 */
public class HttpExample {

    /**
     * @param args the command line arguments
     * @throws java.net.MalformedURLException
     */

    private static String deviceLabel = "my-device";
    private static String variableLabel = "my-variable";
    private static String token = "";
    private static String endpoint = "https://industrial.api.ubidots.com/api/v1.6/devices";
    private static String userAgent = "Java/0.1";

    public static void main(String[] args) throws MalformedURLException, IOException {
        String ubiEndpoint= endpoint + "/" + deviceLabel;
        String testValue = "1";
        URL ubiUrl = new URL(ubiEndpoint);
        String json = "{\"" + variableLabel + "\":" + testValue + "}";

        System.out.println("payload: " + json);

        HttpURLConnection con = (HttpURLConnection) ubiUrl.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", userAgent);
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("X-AUTH-TOKEN", token);
        con.setDoOutput(true);
        con.setDoInput(true);

        try {
            con.setRequestMethod("POST");
            try (OutputStream os = con.getOutputStream()) {
                os.write(json.getBytes("UTF-8"));
            }
            int responseCode = con.getResponseCode();
            System.out.println("response code: " + responseCode);
        } catch (ProtocolException ex) {
            Logger.getLogger(HttpExample.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}