0
Have the following connection class:
public class Conexao {
public static String postDados(String urlUsuario, String parametrosUsuario) {
URL url;
HttpURLConnection connection = null;
try {
url = new URL(urlUsuario);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Lenght", "" + Integer.toString(parametrosUsuario.getBytes().length));
connection.setRequestProperty("Content-Language", "pt-BR");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(parametrosUsuario);
dataOutputStream.flush();
dataOutputStream.close();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String linha;
StringBuffer resposta = new StringBuffer();
while((linha = bufferedReader.readLine()) != null) {
resposta.append(linha);
resposta.append('\r');
}
bufferedReader.close();
return resposta.toString();
} catch (Exception erro) {
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
However when I try to send something by utf-8 like this, for example, it does not get in utf-8:
url = "url";
parametros =
new cadastro.registro().execute(url);
To go, I have to send by get, at the url:
url = "url" + "?texto=" + "~~a@#";
parametros = "";
new cadastro.registro().execute(url);
But I need it to be by POST and not by GET, someone can help me?
private class registro extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return Conexao.postDados(urls[0], parametros);
}
@Override
protected void onPostExecute(String resultado) {
}
}
Try to pass the charset on
Content-Type
:connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
– Homer Simpson
@Henry I will test here now and I will return
– Bestfastfire
@Henry man, it seems that now the paramedics are not going
– Bestfastfire
@Henrique pera, it was yes, I q typed a wrong business here kkkk, but not in utf-8, in mysql the record where it had special character got ? like, j?
– Bestfastfire