2017-02-01 7 views
0

Github APIを使用して特定のリポジトリに問題をオープンしようとしています。 私が直面している問題は、アクセント付きの文字(例:á、é、í、ó、ú)を使用すると、次の例外を除いて要求が失敗することです。アクセント付き文字をREST API(Github Issues)に渡すにはどうすればよいですか?

java.io.FileNotFoundException: https://api.github.com/repos/myorganization/myrepo/issues 

私が試したもの:

String title = "MyTitle"; 
String description = "áé" 
JSONObject jsonBody = new JSONObject(); 
jsonBody.put("title", title); 
jsonBody.put("body", description); 

JSONObject urlParameters = jsonBody; 
int timeout = 5000; 
URL url; 
HttpURLConnection connection = null; 
    try { 
     // Create connection 
     url = new URL(MyConstants.GITHUB_REPO); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type","application/json"); 
     connection.setRequestProperty("Authorization", MyConstants.GITHUB_TOKEN); 
     connection.setUseCaches(false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setConnectTimeout(timeout); 
     connection.setReadTimeout(timeout); 

     // Send request 
     DataOutputStream wr = new DataOutputStream(
       connection.getOutputStream()); 
     wr.writeBytes(urlParameters.toString()); 
     wr.flush(); 
     wr.close(); 

     // Get Response 
     InputStream is = connection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while ((line = rd.readLine()) != null) { 
      response.append(line); 
      response.append('\r'); 
     } 
     rd.close(); 
     is.close(); 
     return response.toString(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

私はAPIに送信されたアクセント付き文字を管理するための適切な方法が何であるかを知っていただきたいと思います。

答えて

2

質問は、あなたは、単にJSON文字列をエンコードすることができ、既にSending UTF-8 string using HttpURLConnection

あなたの場合は答えを持っています。

byte[] buffer = Charset.forName("UTF-8").encode(urlParameters.toString()).array(); 
wr.write(buffer,0,buffer.length); 
関連する問題