我正在使用android studio开发我的移动应用, 而且我目前能够从我的S3存储桶中上传和下载文件 在this guideline之后,现在我想访问我的api网关之一,此api网关用于调用我的lambda python函数,并且我还希望我的应用返回一些有用的信息,我该如何在我的android studio上配置上述设置(我正在使用Android Studio 3.1.4,并且我的手机正在运行android 8.0)
答案 0 :(得分:0)
因此,使用HttpUrlConnection可以使用以下代码以json正文命中您的api。有了这个,我们发出发布请求,并通过JSON发送给您。
public static String SendGet(String testevent, String testeventthing2) throws Exception {
System.out.println("SENDGET");
final String USER_AGENT = "Mozilla/5.0";
String url = "https://yourapiblah.execute-api.us-east-1.amazonaws.com/dev/";
String json = "{\"testevent\":\"" + testevent + "\",\"testeventthing2\":\"" + testeventthing2 + "\"}";
System.out.println("JSON: " + json);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
OutputStream os = con.getOutputStream();
os.write(json.getBytes(StandardCharsets.UTF_8));
os.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return String.valueOf(response);
}