当前位置: 代码迷 >> Android >> android HTTP请求响应的有关问题
  详细解决方案

android HTTP请求响应的有关问题

热度:49   发布时间:2016-05-01 21:40:22.0
android HTTP请求响应的问题
我用android模拟器访问本地的服务器,出现响应不成功
Java code
    // 发送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;    }

其中url是没问题的,我用浏览器访问能够得到正确的结果
但是在android中,他响应不成功
response.getStatusLine().getStatusCode()返回的是405,不是200
请问各位大大,到底是什么原因啊?

------解决方案--------------------
url贴出来啊
------解决方案--------------------
username=username&password=password????
你这里面username和password都放到字符串里面去了,怎么传值进去呢。。。。。
改成:url="http://192.168.1.168:8080/sshop_Server/servlet/LoginServlet?username="+username+"&password="+password;
 

------解决方案--------------------
看你URL应该使用get方法吧、你现在用的post 当然报错了。。。
这是之前写的,你看下

Java code
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里,楼上说的都是这个问题
  相关解决方案