Webページから取得されたJSON配列で奇妙な文字エンコードの問題が発生しています。サーバーはこのヘッダーを返送しています。Android Java UTF-8 HttpClientの問題
コンテンツタイプテキスト/ javascript; charset = UTF-8
また、FirefoxやブラウザのJSON出力やUnicode文字が正しく表示されます。応答には、アクセント記号などを含む別の言語の単語が含まれることがあります。しかし、私はそれをプルダウンしてJavaの文字列に置くと、それらの奇妙な疑問符を得ています。ここに私のコードは次のとおりです。
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf-8");
params.setBooleanParameter("http.protocol.expect-continue", false);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet("http://www.example.com/json_array.php");
HttpResponse response;
try {
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() == 200){
// Connection was established. Get the content.
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String jsonText = convertStreamToString(instream);
Toast.makeText(getApplicationContext(), "Response: "+jsonText, Toast.LENGTH_LONG).show();
}
}
} catch (MalformedURLException e) {
Toast.makeText(getApplicationContext(), "ERROR: Malformed URL - "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "ERROR: IO Exception - "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "ERROR: JSON - "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
あなたが見ることができるように、私はInputStreamReaderの上でUTF-8を指定していますが、私はトーストを介して返さJSONテキストを表示するたびに、それは奇妙な疑問符を持っています。代わりにbyte []にInputStreamを送る必要があると思っていますか?
ご協力いただきありがとうございます。 Arhimedの答え@
if (entity != null) {
// A Simple JSON Response Read
// InputStream instream = entity.getContent();
// String jsonText = convertStreamToString(instream);
String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
// ... toast code here
}
ありがとう:
はその後InputStreamReader
を作成するために、抽出された文字セットを使用します。私はあなたの変更を追加し、EntityUtils用の余分なApacheのものをインポートしましたが、今度はEntityUtils.toString行で予期せず終了します。プログラムをコンパイルして実行しますが、toStringを呼び出す前に何かを実行する必要がありますか? –心配しないでください。私はばかだったし、私のURLで何かを台無しにした。できます!文字は正しくレンダリングされます。 –
@Michael:この回答はとても良いです。私が質問したら、これを受け入れます。 – SK9