2017-03-29 18 views
0

こんにちは、私はJSONオブジェクトでPOSTリクエストを送信しようとしているので、ユーザーを認証するために(現在ローカルに実行されている)APIに接続する必要があるリクエストボディとして、私は、私は次のエラーを取得するログインしようとするたび:アンドロイドデバイスからポストリクエストを送信するときに予期しないステータス行

Unexpected status line: �� 
      java.net.ProtocolException: Unexpected status line: �� 

は、ここに私のコードです:

String API_URL = "http://10.0.2.2:8443/api/"; 
     try { 
      URL url = new URL(API_URL); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      try { 
       urlConnection.setDoOutput(true); 
       urlConnection.setRequestMethod("POST"); 
       urlConnection.setRequestProperty("Content-Type", "application/json"); 
       urlConnection.connect(); 

       JSONObject jsonObject = new JSONObject(); 
       jsonObject.put("customerId", 1); 
       jsonObject.put("password" , mPassword); 
       jsonObject.put("username" , mEmail); 

       DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); 
       wr.writeBytes(jsonObject.toString()); 
       Log.d("REQUEST BODY", jsonObject.toString()); 
       wr.flush(); 
       wr.close(); 

       int response = urlConnection.getResponseCode(); 
       Log.d("RESPONSE", String.valueOf(response)); 
       if(response == HttpURLConnection.HTTP_OK) { 
        InputStream bis = new BufferedInputStream(urlConnection.getInputStream()); 
        return getStringFromInputStream(bis); 
       } 
      } 
      finally{ 
       urlConnection.disconnect(); 
      } 
     } 
     catch(Exception e) { 
      Log.e("ERROR", e.getMessage(), e); 
      return null; 
     } 

誰も私が間違っているかもしれないものを教えていただけますか?ありがとう!

EDIT

このことができますが、問題は、私はurlConnection.getResponseCode();を呼び出すときに発生するようであればわかりません。

答えて

0

おそらくあなたは

+0

を接続する前にurlConnection.setRequestProperty("Connection", "close");を追加接続

試みをリサイクルしている、私は前にこれを試してみたし、それは私に同じエラーを与えます。 – Tuco

+0

この前に他のHTTPコールが発生していますか?もしそうなら、これを使う前に接続が閉じていることを確認する必要があります。 –

+0

これは唯一のHTTPコールです。これは 'AsyncTask'から拡張された' LoginLoginTask'を持つ 'LoginActivity'の中にあります。私はHTTP POST要求を行い、それは私のアプリケーションが持っているものです。私は今のところログイン作業をしようとしています。 – Tuco

関連する問題