开发者

Android httpclient (request) encoding problem

开发者 https://www.devze.com 2023-02-15 04:23 出处:网络
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.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号