Question

I have a problem with diacritic.

When I'm trying to add user on my web manually, everything is saved in database perfectly (also characters like ě,š,č,ř,ž,ý,á,í,é), but when I save String

String diacritic = "ěščřřžžýáíé";

And pass it to getJSONFromUrl in NameValuePair params, it saves to database something like this:

?????

I really don't know how to get this to work

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        HttpParams arg = new BasicHttpParams();
        HttpProtocolParams.setVersion(arg, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(arg, "utf-8");

        // defaultHttpClient
        HttpClient httpClient = new DefaultHttpClient(arg);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        String jsonText = EntityUtils.toString(httpEntity, HTTP.UTF_8);

        try {
            jObj = new JSONObject(jsonText);            
        } catch (JSONException e) {
            Log.w("JSON Parser", "Error parsing data " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return jObj;
}

Saving like this:

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", "fb_register"));
    params.add(new BasicNameValuePair("diacritic", "ěščřžýáíé"));
    JSONObject json = jsonParser.getJSONFromUrl(URL, params);

Also tried

Charset.forName("UTF-8").encode(diacritic);
params.add(new BasicNameValuePair("diacritic", diacritic));
Was it helpful?

Solution

SOLVED !

// defaultHttpClient
    HttpClient httpClient = new DefaultHttpClient(arg);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); //add encoding
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top