当前位置: 代码迷 >> Android >> android客户端与服务器servlet通信的一个有关问题,求解惑。
  详细解决方案

android客户端与服务器servlet通信的一个有关问题,求解惑。

热度:93   发布时间:2016-05-01 21:44:17.0
android客户端与服务器servlet通信的一个问题,求解惑。。。
这是android里的程序:
Java code
TextView t=(TextView)Select.this.findViewById(R.id.textView00);        String strResult;        String uriAPI="http://10.255.153.46:8080/GetInfor/servlet";        HttpPost httpPostRequest=new HttpPost(uriAPI);        List<NameValuePair> httpParams = new ArrayList<NameValuePair>();        httpParams.add((NameValuePair) new BasicNameValuePair("information", s));    //设置post参数        try {            httpPostRequest.setEntity(new UrlEncodedFormEntity(httpParams,HTTP.UTF_8));            HttpResponse httpResponse = new DefaultHttpClient().execute(httpPostRequest);            Log.d("Infor", "post:"+httpResponse.getStatusLine().getStatusCode());            if(httpResponse.getStatusLine().getStatusCode()==200){        //连接成功                String result = EntityUtils.toString(httpResponse.getEntity());    //获得资源                result = result.replaceAll("\r\n|\n\r|\r|\n", "");                                    t.setText(result);                                                                }        } catch (Exception e) {                                        //捕获并打印异常                    //获得EditText对象            t.setText("连接出错:"+e.getMessage());                                    }

这是servlet里的post方法:
Java code
public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.print("ziyanguang:"+out);        out.flush();        out.close();    }


问题是现在怎么连接不上服务器啊,小弟新手,呵呵,拜求大侠们能够指点下。感激不尽、感激不尽。。。

------解决方案--------------------
如果是真机,手机和电脑要在同一个网络(比如连上同一个wifi热点),手机才能访问tomcat。如果是模拟器,可能就是你的代码问题了。
另外,提高结帖率有助于你连上servlet!哈哈
------解决方案--------------------
先看看你的客户端能不能进行正常的上网,再看看pc上能不能连上你的servlet。两者都可以了,再进行试验吧
------解决方案--------------------
代码没问题,应该是网络方面的问题吧,就像1楼和2楼所说情况
  相关解决方案