Search code examples
androidhttputf-8special-characters

Android http post with special character


my php-site is echoing "här är du/x vår i sikte" which is shown as "här är du/x vår i sikte" in my chrome browser.

But when I read it with httppost in my java/android app all the special characters (åäö) is shown as a questionmark. My webhosting service uses UtF-8. Is there a way to convert my string?

Here is my code:

StringBuffer response = new StringBuffer();
try {
    // Create a new HTTP Client
    DefaultHttpClient defaultClient = new DefaultHttpClient();
    // Setup the get request
    HttpPost post = new HttpPost("my.php");
    try {
        post.setHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");

        MultipartEntityBuilder builder = MultipartEntityBuilder
                .create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        final HttpEntity entity = builder.build();
        post.setEntity((HttpEntity) entity);

        //builder.addTextBody("rubrik_nr", all_rubrik, ContentType.TEXT_PLAIN);     
        HttpResponse resp = defaultClient.execute(post);
    }catch(Exception E){

    }
    // Execute the request in the client
    HttpResponse httpResponse = defaultClient
            .execute(post);

    BufferedReader in = new BufferedReader(new InputStreamReader(
            httpResponse.getEntity().getContent(),"UTF-8"));
    String inputLine;

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
Log.d("DEBUG", "response: " + response.toString());


} catch (IOException e) {
Log.d("DEBUG", "j " + response.toString());
e.printStackTrace();
}

return response.toString();
}

Solution

  • First of all the variable i is out of scope inside of your listener, so you can't use it.

    Second, remember that the click listener will be called after your for loop is complete, therefore using the variable i would be impossible (it no longer exists). However when you declare a final variable, it will be available inside the listener, at all times. You won't be able to change it's value though.

    for(int i=0;i<end;i++)
    {   
        //bilder
        ImageView replace_img= new ImageView(this);
        replace_img.setImageResource(R.drawable.image_replace);
        RelativeLayout.LayoutParams tmp= new RelativeLayout.LayoutParams(250,250);
        newImg.add(replace_img);                
        ImageView v=  newImg.get(i);
        v.setId(i+1);
    
        // Set the final int that will be available in inner classes
        final int myInt = i;
    
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vi) {            
                vi.setOnClickListener(new View.OnClickListener() 
                { 
                    @Override
                    public void onClick(View v) 
                    { 
                        // "myInt" is still usable here, however "i" is not
                        Intent o = new Intent(getBaseContext(), Edit_post2.class);
                        o.putExtra("POST_NR", myInt);
                        Post_list.this.startActivity(o); 
                    }
                });
            }
        });
    }