how to resolve accent errors when sending an Android message to a php API

Asked

Viewed 1,017 times

-1

Guys, I’m having trouble sending strings with accentuation and strings with 2 lines or more. When sending without accentuation or and with only one line it normally accepts, however, when sending with accentuation, it error.

I’ve used an API tester (Easy Rest), and when sending there, it accepts normally and shows no error, but when sending by Android, error...

this is the part of the code related to sending..

public void enviaDados() {
    new LongOperation().execute("");
}

private class LongOperation extends AsyncTask<String, Void, String> {

    String msg = mensagem.getText().toString();
    String mail = email.toString();

    ProgressBar carrega = (ProgressBar) findViewById(R.id.carregaMsg);

    @Override
    protected String doInBackground(String... params) {
        try {
            Log.v("GG", "Sending sever 1 - try");
            // start - line is for sever connection/communication
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http://www.meuSite.com.br/mural/listener.php");

            String json = "{\"usuario\":[{\"enviado\":\"" + "1" + "\",\"email\":\"" + mail.toString() + "\",\"msg\":\"" + msg.toString() + "\",\"a\":\"" + "2" + "\"}]}"; //msg.toString()

            StringEntity entity1 = new StringEntity(json);
            httppost.setEntity(entity1);
            //informa o tipo de arquivo. . .
            httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Content-type", "application/json");

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            // end - line is for sever connection/communication
            InputStream is = entity.getContent();
            //_______________________________________________________________________
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            result = sb.toString();
            is.close();
            jsonObject = new JSONObject(result);
            //__________________________________________________________________________

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection "
                    + e.toString());

        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        carrega.setVisibility(View.INVISIBLE);
        try {
            alerta();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mensagem.setText("");
    }

    @Override
    protected void onPreExecute() {
        carrega.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

If anyone can help me, I’d really appreciate it. Every tip is welcome, thank you for your attention!

hug!

  • You’re making a mistake?

  • 3

    Which enconding (charset) is the server using? Rest Easy works because it uses the same charset configured in the Rest application that receives the data on the server. You need to set the same charset in your Android application. Ser for UTF-8, try doing: httppost.setHeader("Content-type", "application/json; charset=UTF-8");

  • Thanks for the tip, I will try here now, if I succeed or not, I warn you !

  • Thanks for the tip, I tried this way but unfortunately it still does not recognize when I put accents or more of a line, it presents the same error. If anyone has any other idea to resolve this issue, please send.

  • 1

    Tche, I managed to solve here I used your tip, but for it to work, I had to change the Stringentity entity1 line = new Stringentity(json); It looked like this: Stringentity entity1 = new Stringentity(json, "utf-8"); that solved the accents now I just have to solve the line break , for example, type a message, give enter on the phone and continue typing in the second line , it ends up breaking the json in 2 lines instead of recognizing an n for example Now I’ll just figure out how to solve this hehe Rigadão!

  • I solved this line adding android:singleLine="true" in Editi text XML Script Valew guys and thanks for your attention!

Show 1 more comment

1 answer

-4

Try to put the \n followed by the following line without spaces.

Example:

"Olá!\nTudo bem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.