[英]How to send HTTP Post Request with a value in Java (Android Studio) to Ubidots
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
//這是我的代碼。 我正在嘗試向 Ubidots 發出 HTTP post 請求。 如何將帶有 Java (Android Studio) 值的 HTTP Post 請求發送到 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\"}");
}
}
//這是我的代碼,我無法找出錯誤
下面的代碼應該可以解決問題
/*
* 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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.