android HTTP请求响应的问题
我用android模拟器访问本地的服务器,出现响应不成功
// 发送Post请求,获得响应查询结果 public static String queryStringForPost(String url){ // 根据url获得HttpPost对象 HttpPost request = HttpUtil.getHttpPost(url); String result = null; try { // 获得响应对象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判断是否请求成功 if(response.getStatusLine().getStatusCode()==200){ // 获得响应 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "网络异常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "网络异常!"; return result; } return null; }String uriAPI="http://10.255.153.46:8080/Registe/servlet/reg?user="+s0+"&password="+s1; String strResult; HttpGet httpRequest=new HttpGet(uriAPI);//创建HTTP Get连接 try { HttpClient client = new DefaultHttpClient(); client.execute(httpRequest); HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest); if(httpResponse.getStatusLine().getStatusCode()==200){ //当访问服务器有效时 strResult=EntityUtils.toString(httpResponse.getEntity()).trim(); Log.d("Infor","新用户:"+strResult); if(strResult.equals("Y")){ new AlertDialog.Builder(Register1.this).setTitle(" 提示") .setMessage("注册成功!!") .setPositiveButton("确定", null) .show();} else if(strResult.equals("N")){ new AlertDialog.Builder(Register1.this).setTitle(" 提示") .setMessage("注册失败,该账号已被使用!!") .setPositiveButton("确定", null) .show();} } else{ //System.out.println(httpResponse.getStatusLine().getStatusCode()); Log.d("Infor","连接失败"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println(e); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println(e); }
[解决办法]
代码没问题,但是我没看到你在httpPost那里哪边添加参数值了啊
要是代码中的方法的参数url为你debug中的值,貌似这是HttpGet方式请求的啊,httpPost就改成httpGet
如果你一定要用post方式,需要httpPost.setEntity(HttpEntity entity);
[解决办法]
post请求的时候参数不应该设置在url里,楼上说的都是这个问题