我正在做一个Android应用程序,我只是做一个获取http请求,我只是想知道我做得不好。
这是我的要求:
try {
String encodedUserNAme = idDispositivo;
encodedUserNAme = URLEncoder.encode(encodedUserNAme, "utf-8");
String url = Constants.URL_OBTENER_GRUPO + "?" + "user="+ idDispositivo;
Log.w("ObtenerIDGrupo "," url: "+url);
HttpClient client = Constants.getHttpClient();
HttpGet httpget;
try {
httpget = new HttpGet(new URI(url));
try{
HttpResponse response = client.execute(httpget);
String responseBody = EntityUtils.toString(response.getEntity());
Log.w("ObtenerIDGrupo "," RESPONSEBODY= "+responseBody);
idGrupo = responseBody;
Log.w("ObtenerIDGrupo "," idGrupo= "+responseBody);
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
Log.w("IOException" + e);
e.printStackTrace();
}
这是我的回答:
我不知道为什么回答是这样的。 请有人帮忙吗?
由于
答案 0 :(得分:1)
您无法像这样操纵响应,请尝试以下代码:
InputStream is = response.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(is));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
str.append(line + "\n");
}
responseBody = str.toString();
Log.w("ObtenerIDGrupo "," RESPONSEBODY= "+responseBody);
答案 1 :(得分:0)
试试这个
URL url = new URL(yoururl);
InputStream input=url.openStream();
BufferedInputStream bis=new BufferedInputStream(input);
ByteArrayBuffer baf=new ByteArrayBuffer(1000);
while((k=bis.read())!=-1)
{
baf.append((byte)k);
}
String responsedata=new String(baf.toByteArray());