【发布时间】:2015-04-29 07:14:20
【问题描述】:
我已尝试使用此代码 sn-p 使用 HTTPResponse 从 android 获取响应
String s="";
try {
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://10.0.0.8:7777/HttpPostServlet/servlet/Login");
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("uname", valuse[0]));
list.add(new BasicNameValuePair("pwd",valuse[1]));
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse httpResponse= httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
String responseStr = EntityUtils.toString(httpResponse.getEntity());
s= readResponse(httpResponse);
System.err.println("Response: " + s +"/// "+ "another resposn :: "+responseStr );
} catch(Exception exception) {
System.err.println("Exception: " + exception.getMessage());
}
我也尝试了另一种方法来获得响应
public String readResponse(HttpResponse res) {
InputStream is=null;
String return_text="";
String result="";
try {
is=res.getEntity().getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
String line="";
StringBuffer sb=new StringBuffer();
HttpEntity entity = res.getEntity();
while ((line=bufferedReader.readLine())!=null) {
sb.append(line);
}
return_text=sb.toString();
result = EntityUtils.toString(entity).toString();
} catch (Exception e) {}
return result;
}
我收到如下回复
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://23.253.164.20:8096/">Unsuccessfull</string>
我只想要不成功或成功
我是否必须在服务器端进行更改,或者可以在客户端完成以获取纯文本
【问题讨论】:
-
使用像Volley这样的库
-
使用 volley 是另一回事.. 我能得到解决方案吗?
标签: android http-post httpresponse webmethod