【问题标题】:Android httpclient (request) encoding problemAndroid httpclient(请求)编码问题
【发布时间】:2011-03-05 15:57:16
【问题描述】:

嗨 我正在尝试在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);  
    } 
}

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

【问题讨论】:

    标签: android rest httpclient


    【解决方案1】:

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

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

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

    HTTPGet extends this class with header methods

    Source info on xml encoding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      相关资源
      最近更新 更多