2
Gentlemen, good morning/afternoon/evening!
I’m having trouble accentuating words. The back-end of my system works in PHP chatting with an Android app. When I send the App message to the PHP server it works normally, and PHP responds as expected, but if the message sent to PHP includes accented characters the App shows error and to.
I have already researched of course, but most people are using Httpclient and I am using Httpurlconnection.
I already tried to include a line saying that Requestproperty is UTF-8, but it did not help.
Follows Connection class I’m using.
package Conexao;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by billy on 21/05/2016.
*/
public class Conexao {
private static Conexao instancia;
public static String urlString;
private Conexao() {
//nothing
}
public static Conexao getInstancia() {
if (instancia == null) {
instancia = new Conexao();
}
return instancia;
}
public static JSONArray executePost(JSONObject json) {
JSONArray jsonArray = null;
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("contentType", "application/json");
conn.connect();
conn.setConnectTimeout(1000);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(json.toString());
InputStream is = conn.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
jsonArray = new JSONArray(responseStrBuilder.toString());
} catch (Exception e) {
Log.e("Erro ", e.toString());
}
Log.i("Resposta: ", jsonArray.toString());
return jsonArray;
}
}
From now on, thank you.
Gentlemen, as requested, follow the logcat:
09-22 20:01:39.959 25838-25838/? E/Zygote: v2
09-22 20:01:39.969 25838-25838/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
09-22 20:02:06.999 25838-26110/br.com.infogruposi.sct E/Erro: org.json.JSONException: Value Nenhum of type java.lang.String cannot be converted to JSONArray
09-22 20:02:07.019 25838-26110/br.com.infogruposi.sct E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: br.com.infogruposi.sct, PID: 25838
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONArray.toString()' on a null object reference
at Conexao.Conexao.executePost(Conexao.java:68)
at br.com.infogruposi.sct.ActAbreChamadoUsu$JSONTransmitter.doInBackground(ActAbreChamadoUsu.java:47)
at br.com.infogruposi.sct.ActAbreChamadoUsu$JSONTransmitter.doInBackground(ActAbreChamadoUsu.java:42)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Gentlemen, I’m doing tests and the following excerpt I removed from my debug:
json = {JSONObject@18906} "{"cha_gru_codigo":"1","cha_pes_usuario":"3","cha_solicitacao":"zé","cha_des_texto":"zinho","classe":"chamado","acao":"usuAbrirChamado"}"
jsonArray = null
url = {URL@18958} "http://www.solusinformatica.890m.com/SCT/SCT/facade/Facade.php"
conn = {HttpURLConnectionImpl@18961} "com.android.okhttp.internal.http.HttpURLConnectionImpl:http://www.solusinformatica.890m.com/SCT/SCT/facade/Facade.php"
wr = {DataOutputStream@18978}
is = {RealBufferedSource$1@18992} "buffer(com.android.okio.GzipSource@2be6139).inputStream()"
streamReader = {BufferedReader@18997}
responseStrBuilder = {StringBuilder@19002} ""
inputStr = {String@19007} "Nenhum comando selecionado - <pre>null</pre>"
This last inputStr field is the variable that received the response from Backend and the field between the pre tags, with null value is what the backend received from the Android App, IE, when the message has accent the backend does not receive the data, I believe because Android simply does not send them.
I tried to apply the solution described in the post:
The App this time sent the message, but PHP received it like this: zé = z%C3%A9
What error appears in logcat when it stops?
– Laerte
Forgive me, friends, the error is when the App tries to send an accent message to PHP. Simply the message does not leave the App. As for the mistake, I’ll need to do it tonight when I’m home, because I don’t remember. I’ve been trying to figure this out for so long that I’d given up.
– Billly Jow
@Billlyjow Welcome to Stack Overflow!! Edit your question with this new information to make it easier for others who read your question!
– Giovanni Bernini
I made the suggested changes and, if necessary, I can also post the class that controls the view that receives user data.
– Billly Jow
This "None" in the fourth line comes from PHP that responds "No commands selected" when the front does not send the necessary information to Facade.
– Billly Jow
Ho, and now, who can help me?
– Billly Jow