2016-08-28 5 views
0

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のいずれかとして添付する必要がありますか。誰でも私を助けてもらえますか?事前に感謝します。

+0

あなたのケースにはどのようなリクエストタイプがありますか? – Imdad

+0

あなたのログを共有できますか – cafebabe1991

+0

GETがリクエストです@ Jack'sretardedcode – karthickraja

答えて

0

ライブラリを使用するOkHttpClientネットワークコール用。

ライブラリーは、より簡単に使用できます。

があなたのbuild.gradle

compile 'com.survivingwithandroid:weatherlib_okhttpclient:1.6.0' 

に依存関係を追加今OkHttpClientライブラリとネットワーク呼び出し

OkHttpClient client = new OkHttpClient(); 
Request request = new Request.Builder() 
    .url("http://ec2-52-88-152-29.us-west-2.compute.amazonaws.com/api/values") 
    .get() 
    .addHeader("cache-control", "no-cache") 
    .build(); 

Response response = client.newCall(request).execute(); 
String jsonData = response.body().string(); 

またあなたのAPI呼び出しのためのOkHttpClientコードを生成しますChromeの拡張機能postmanがありますを行います。

+0

すでに私はvolleyライブラリの使用を試みた。しかし、私はそれがなぜHttpUrlConnectionにないのか知りたい。とにかくありがとう@Yogesh – karthickraja

関連する問題