Gabriel Tavares
unread,Jun 2, 2011, 6:31:02 AM6/2/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to java.ce
Olá amigos, estou enviando e recebendo dados, via post, para/de uma
central feita usando linguagem php. Porém tenho q enviar e receber
Strings usando caracteres especiais, então quando faço isso a string é
chega de forma inválida. Por exemplo: "avanço", eu recebo somente
"avan".
segue o código:
//
*********************************************************************************************
public String doPost(String url, String params, String
contentType){
try {
URL u = new URL(url);
HttpURLConnection connection = (HttpURLConnection)
u.openConnection();
connection.setRequestMethod(Constantes.RESQUEST_METHOD_POST);
connection.setRequestProperty("User-Agent", Constantes.HTTP_AGENT);
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Connection", "close");
connection.setRequestProperty("Content-Type", contentType);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
OutputStream out = connection.getOutputStream();
byte[] paramsBytes = params.getBytes(Constantes.CODIFICACAO_UTF8);
out.write(paramsBytes);
out.flush();
out.close();
InputStream in = connection.getInputStream();
String resp = readString(in);
connection.disconnect();
return resp;
} catch (MalformedURLException e) {
Log.e(Constantes.CATEGORIA, e.getMessage(), e);
} catch (ProtocolException e) {
Log.e(Constantes.CATEGORIA, e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
Log.e(Constantes.CATEGORIA, e.getMessage(), e);
} catch (IOException e) {
Log.e(Constantes.CATEGORIA, e.getMessage(), e);
}
return "";
}
private byte[] readBytes(InputStream in) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) > 0){
bos.write(buffer, 0, len);
}
byte[] bytes = bos.toByteArray();
return bytes;
} finally{
bos.close();
in.close();
}
}
private String readString(InputStream in) throws IOException {
byte[] bytes = readBytes(in);
String texto = new String(bytes);
Log.i(Constantes.CATEGORIA, "Http.readString: " + texto);
return texto;
}
//
*********************************************************************************************
Existe alguma forma de contornar esse problema??
=]