Android httpclient(请求)编码问题

发布于 2024-10-20 14:28:25 字数 1632 浏览 2 评论 0原文

你好 我正在尝试在 android 中构建一个小的休息客户端。我只是尝试获取一个稍后可以解析的 xml 文件。但是我有一些编码问题。

无法识别 ø 和 å 等特殊字符。 xml 文件使用 ISO-8859-1 编码,但我无法真正弄清楚如何强制 httpclient 使用此编码。有谁能帮忙吗?

这是代码:

    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    String URL = "http://konkurrence.rejseplanen.dk/bin/rest.exe"; 

    String result = ""; 

    Button btn; 
    TextView tv; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        tv = (TextView)findViewById(R.id.tvResponse); 
        btn = (Button)findViewById(R.id.btnMakeRequest); 

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String query = "/departureBoard?id=8600626&date=19.03.11&time=07:02&useBus=0"; 
                callWebService(query); 
            }
        }); 
    }

    public void callWebService(String q){  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpGet request = new HttpGet(URL + q);   
        ResponseHandler<String> handler = new BasicResponseHandler();  
        try {  
            result = httpclient.execute(request, handler); 
            tv.setText(result); 
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        httpclient.getConnectionManager().shutdown();  
        Log.i("test", result);  
    } 
}

提前致谢。 最好的问候,肯尼思

Hi
I am trying to build a little rest client in android. I simply tries to obtain an xml file which can be parsed later on. However I have some encoding problems.

Special characters like ø and å are not recognized. The xml file uses ISO-8859-1 encoding but i cannot really figure out how to force the httpclient to use this encoding. Anyone that is able to help?

Here is the code:

    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    String URL = "http://konkurrence.rejseplanen.dk/bin/rest.exe"; 

    String result = ""; 

    Button btn; 
    TextView tv; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        tv = (TextView)findViewById(R.id.tvResponse); 
        btn = (Button)findViewById(R.id.btnMakeRequest); 

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String query = "/departureBoard?id=8600626&date=19.03.11&time=07:02&useBus=0"; 
                callWebService(query); 
            }
        }); 
    }

    public void callWebService(String q){  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpGet request = new HttpGet(URL + q);   
        ResponseHandler<String> handler = new BasicResponseHandler();  
        try {  
            result = httpclient.execute(request, handler); 
            tv.setText(result); 
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        httpclient.getConnectionManager().shutdown();  
        Log.i("test", result);  
    } 
}

thanks in advance.
best regards, kenneth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-10-27 14:28:25

我会看一下响应的标题。响应需要设置:

Content-Type: text/xml; charset:ISO-8859-1;

否则,我的理解是http客户端将默认编码为utf-8。如果您的网络服务正在使用请求头来尝试确定您想要的内容,您可能还需要调整请求的标头。需要知道的是,如果您在浏览器中执行此操作,您会返回 iso 文档还是 utf-8 文档?

HTTPGet 使用标头方法扩展了此类

有关 xml 编码的源信息

I would take a look at the response's headers. The response will need to set :

Content-Type: text/xml; charset:ISO-8859-1;

Otherwise, my understanding is that the http client will default encode to utf-8. You may also need to adjust the header on your request if your web service is using it to try and determine what you want. The thing to know is, if you do this in a browser do you get back the iso document or a utf-8 one?

HTTPGet extends this class with header methods

Source info on xml encoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文