मेरे पास एक वेब पेज से पकड़े गए JSON सरणी के साथ अजीब वर्ण एन्कोडिंग समस्याएं हैं। सर्वर इस हेडर को वापस भेज रहा है:एंड्रॉइड जावा यूटीएफ -8 एचटीपी क्लाइंट समस्या
सामग्री-प्रकार टेक्स्ट/जावास्क्रिप्ट; charset = UTF-8
इसके अलावा मैं फ़ायरफ़ॉक्स में JSON आउटपुट या किसी भी ब्राउज़र और यूनिकोड वर्णों को ठीक से प्रदर्शित कर सकता हूं। प्रतिक्रिया में कभी-कभी उच्चारण भाषा के साथ किसी अन्य भाषा के शब्द होते हैं। हालांकि, जब मैं इसे नीचे खींचता हूं और इसे जावा में एक स्ट्रिंग में डालता हूं तो मुझे उन अजीब प्रश्न चिह्न मिल रहे हैं।
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 टेक्स्ट देखने यह अजीब प्रश्न चिह्न है: यहाँ मेरी कोड है। मैं सोच रहा हूं कि मुझे इनपुटस्ट्रीम को बाइट [] के बजाय भेजने की ज़रूरत है?
किसी भी मदद के लिए अग्रिम धन्यवाद।
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 के लिए अतिरिक्त अपाचे सामग्री आयात की लेकिन अब ऐप केवल EntityUtils.toString लाइन पर अप्रत्याशित रूप से समाप्त हो जाता है। प्रोग्राम संकलित करता है और चलता है, लेकिन क्या मुझे स्ट्रिंग को कॉल करने से पहले इकाई को कुछ करने की ज़रूरत है? –कोई बात नहीं। मैं एक मूर्ख था और मेरे यूआरएल के साथ कुछ गड़बड़ कर दिया। यह काम करता हैं! पात्रों को सही ढंग से प्रस्तुत किया जाता है! –
@ माइकल: यह जवाब बहुत अच्छा है और अगर मैं सवाल पूछता तो मैं इसे स्वीकार करूंगा। – SK9