2017-03-26 91 views
0

私は単純なログインページを行っています。私はユーザー名フィールドとパスワードフィールドを送信しています。私はこれらのパラメータが正しく渡されていることをサーバー上で確認し、サーバーはjson応答を返します。 HTTPが200ならば、私は応答を解析し、それは正常に動作するメッセージを取得します。メッセージを解析しようとすると状態が401の場合、すぐにIOExceptionが発生します。サーバーからの応答を解析できません

router.use(function(err, req, res, next) { 
    console.log('Message returned ' + err.status + " " + err.message); 
    res.status(err.status || params.ERROR_HTTP_INTERNAL).json(JSON.stringify({ 
     error: err.message 
    })); 
}) 

私はそれが郵便配達を使用するときため、サーバーとは何かを持っているとは思わない:

protected String doInBackground(String... params) { 
      HttpURLConnection conn = null; 
      try { 

       URL url = new URL("http://192.168.1.91:3000/api/login"); 

       JSONObject postDataParams = new JSONObject(); 

       postDataParams.put("username", getUsername()); 
       postDataParams.put("password", getPassword()); 

       Log.e("params", postDataParams.toString()); 

       conn = (HttpURLConnection) url.openConnection(); 
       conn.setReadTimeout(30000 /* milliseconds */); 
       conn.setConnectTimeout(30000 /* milliseconds */); 
       conn.setRequestMethod("POST"); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 
       conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
       conn.setChunkedStreamingMode(0); 

       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       writer.write(getPostDataString(postDataParams)); 

       writer.flush(); 
       writer.close(); 
       os.close(); 

       int responseCode = conn.getResponseCode();  

       Log.e("responseCode", ""+responseCode); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 

        InputStream in = new BufferedInputStream(conn.getInputStream()); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
        String line = ""; 

        while ((line = reader.readLine()) != null) { 
         result.append(line); 
        } 

        in.close(); 
        return result.toString(); 

       } 
       else if (responseCode == HttpsURLConnection.HTTP_UNAUTHORIZED) { 

        InputStream in = new BufferedInputStream(conn.getInputStream()); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
        String line = ""; 

        while ((line = reader.readLine()) != null) { 
         result.append(line); 
        } 

        in.close(); 
        return result.toString(); 

       } 
       else { 
        return new String("false : " + responseCode); 
       } 
      } catch (MalformedURLException ex) { 
       return new String("false : MalformedURLException "); 
      } catch (IOException ex) { 
       return new String("false : IOException "); 
      } catch (JSONException e) { 
       return new String("false : JSONException "); 
      } catch (Exception e) { 
       return new String("false : Exception1 "); 
      } finally { 
       if (conn != null) { 
        try { 
         conn.disconnect(); 
        } catch (Exception ex) { 
         return new String("false : Exception2 "); 
        } 
       } 
      } 
     } 

サーバーは関係なく、HTTPのステータスが何であるかを、次の応答を返さないnodejsサーバがありますREST呼び出しを作成する私は適切な応答コードを取得しており、JSON応答を見ることができます。

答えて

0

A 400は、あなたのリクエストに何か間違っていたことを意味します。その場合、戻ってきた身体はありません。サーバーが400を返送する理由(ログインデータが無効な場合は400を送信するのか)を調べる必要があります。とにかく、400でボディを解析しようとしないで、エラーとして扱います。

+0

サーバは400コードを送信しますが、それは身体を送信していることと一緒にされています。これに

InputStream in = new BufferedInputStream(conn.getInputStream()); 

:それは私がこれを変更する必要がリターンされる誤差であるので、 JSONオブジェクトエラー: "some message"郵便配達員のような別のプログラムを使って休息をとると、身体が含まれているのがわかるので、これを確認できます。私はエラーフィールド – user2924127

-2

方が良いRESTAPI

ためokhttpライブラリを使用したいと私は答えを見つけた非同期

+0

に入ることができる必要があります私はライブラリの利点は何ですか?私はむしろ公式アンドロイドライブラリHttpURLConnectionを使用することを好むでしょう。このコードはAsyncTaskで実行されています – user2924127

0

を勉強することをお勧めします。

InputStream in = new BufferedInputStream(conn.getErrorStream()); 
関連する問題