当前位置:编程学习 > wap >>

接收不到服务器返回的数据

private void login() {
// 创建HttpPost对象,其主要作用是将Android手机端的数据提交给Web应用服务器
HttpPost httpRequest = new HttpPost(URL);

// 将要传递的参数保存到List集合中
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname", userName.getText().toString()));
params.add(new BasicNameValuePair("upswd", password.getText().toString()));

try {
// 设置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
httpRequest.setEntity(entity);
// 执行连接
HttpResponse response = httpClient.execute(httpRequest);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//获取返回的数据
result += EntityUtils.toString(response.getEntity(), "utf-8");
Log.e("服务返回数据:", result);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

对result做了下Log,没有反应,是不是我BasicNameValuePair的key或者是一开始的URL错了。就算我登录的时候账号密码有误,Log怎么也该显示下服务器返回的数据result啊。求指教! 服务器 url result  BasicNameValuePair 返回数据 --------------------编程问答-------------------- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {}
加上else打印,你和服务器的通信成功,也就是返回HttpStatus.SC_OK(200)才会执行if中的代码,如果不是200呢?你没做任何处理的
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,