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();
}
}
}
}
But when I try to send something by utf-8, for example, it does not stay in utf-8:
url = "url";
parametros =
new cadastro.registro().execute(url);
To go, I have to send by get, in the url:
url = "url" + "?texto=" + "~~a@#";
parametros = "";
new cadastro.registro().execute(url);
But I need it to be for POST and not for GET, can anyone 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) {
}
}