AWS EC2
によって作成されたアンドロイドアプリのAPIにバックエンドとしてアクセスできません。私が使用しているAWS EC2によってバックエンドとして作成された自分のアンドロイドアプリのAPIにアクセスできない
URLはここで "http://ec2-52-88-152-29.us-west-2.compute.amazonaws.com/api/values"
が私のコードです。
public class ServiceHelper {
JSONObject JSONresponseText;
Context context;
JSONObject jsonResultText;
public ServiceHelper(Context context) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
this.context = context;
}
public JSONObject jsonSendHTTPRequest(String requestData,
String requestURL, String requestType) {
try {
if (!Constants.isConnectingToInternet(context)) {
JSONresponseText = new JSONObject();
JSONresponseText.put(Constants.KEY_ERROR,
Constants.TOAST_INTERNET);
} else {
HttpURLConnection connection = null;
try {
Log.e("request data", requestData + Constants.EMPTY_STRING
+ requestURL);
URL object = new URL(requestURL);
connection = (HttpURLConnection) object.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(requestType);
connection.setRequestProperty(Constants.KEY_CONTENT_TYPE,
Constants.VALUE_CONTENT_TYPE);
connection.setRequestProperty(Constants.KEY_ACCEPT,
Constants.VALUE_CONTENT_TYPE);
if (requestType
.equalsIgnoreCase(Constants.POST_METHOD)) {
OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
streamWriter.write(requestData);
streamWriter.flush();
}
StringBuilder stringBuilder = new StringBuilder();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader streamReader = new InputStreamReader(
connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
streamReader);
String response = null;
while ((response = bufferedReader.readLine()) != null) {
stringBuilder.append(response + "\n");
}
bufferedReader.close();
Log.d("Result Value ", stringBuilder.toString());
jsonResultText = new JSONObject(
stringBuilder.toString());
return jsonResultText;
} else {
Log.e("ServiceHelper", connection.getResponseMessage());
return null;
}
} catch (Exception exception) {
Log.e("ServiceHelper", exception.toString());
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
} catch (Exception e) {
Log.e("ServiceHelper", e.toString());
}
return JSONresponseText;
}
}
ログの値は、私がNo Content
として応答を得た
08-28 22:18:39.282 21499-21499/com.demo.demoaws D/Response Code: 204
08-28 22:18:39.282 21499-21499/com.demo.demoaws E/ServiceHelper: No Content
です。私は何が問題なのか分からない。ヘッダーはAWS credentials
のいずれかとして添付する必要がありますか。誰でも私を助けてもらえますか?事前に感謝します。
あなたのケースにはどのようなリクエストタイプがありますか? – Imdad
あなたのログを共有できますか – cafebabe1991
GETがリクエストです@ Jack'sretardedcode – karthickraja