2016-06-15 5 views
0

APIを実行しようとしましたが、json応答を取得しようとしていますが、bf.readLine()の場合は"Disallowed Key Characters"とエラーが表示されます。json出力の「許可されないキー文字」エラー

以下は、私が使用しようとしているコードです。しかし、WebブラウザでリクエストURLを実行すると、問題なしで応答が得られます。しかし、Javaコードを使用するとデータを抽出できません。あなたが適切な形式で任意の特殊文字をエンコードすることができによってBufferedReader符号化技術を追加する必要があるため

String uri = "http://192.168.77.6/Ivr_ABN_API/?id?id=" 
      + mobile; 
    URL url; 
    Gson json = null; 
    try { 
     url = new URL(uri); 
     json = new Gson(); 

     HttpURLConnection connection; 
     access_token = db.getAccessTokenFromDB(); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 

     System.out.println("URL:" + uri); 

     connection.setRequestProperty("Content-Type", "application/json"); 
     int status = connection.getResponseCode(); 
     resCode = Integer.toString(status); 


       InputStream in = connection.getInputStream(); 
       BufferedReader bf = new BufferedReader(
         new InputStreamReader(in)); 
       System.out.println("bf.readLine() - "+bf.readLine()); 

       while ((output = bf.readLine()) != null) { 
        JSONObject obj = new JSONObject(output); 
        System.out.println("output is "+output); 
        resCode = obj.getString("resCode"); 
        resDesc = obj.getString("COUNT"); 

       } 


     connection.disconnect(); 
+0

あなたは 'InputStreamReader'の文字セットを指定していません。 JSONは通常UTF-8を使用します。また、JSONは行指向ではないので、おそらく 'readLine()'はおそらく使用する正しい方法ではありません。 'Gson.fromJson()'を代わりに使用することを考えてください。これは 'Reader'を入力として受け取ります。 –

+0

こちらをご覧ください>> https://stackoverflow.com/questions/4964640/reading-inputstream-as-utf-8 –

答えて

0

この

BufferedReader in = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); 

代わりの

BufferedReader bf = new BufferedReader(new InputStreamReader(in)); 

を試してみてください助けてください。

これはあなたに役立つと思います。

ありがとうございます。

0

また、あなたの参照のための質問と回答の解決策が見つかりました。

HttpURLConnection connection; 
connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/json"); 
     connection.setRequestProperty("Content-Type", "application/json"); 
     int status = connection.getResponseCode(); 
BufferedReader bf = new BufferedReader(new InputStreamReader(
       connection.getInputStream())); 
関連する問題