final String username = usernameEditText.getText().toString().trim();        final String password = passwrodEditText.getText().toString().trim();        //Android默认模拟器外部的地址为10.0.2.2,而不是localhost和127.0.0.1        final String serverPath = "http://10.0.2.2:8080/LoginServlet";        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {            //给出提示:账号密码不许为空        } else {            new Thread(new Runnable() {                @Override                public void run() {                    try {                        //创建浏览器                        HttpClient httpClient = new DefaultHttpClient();                        //创建一个post请求对象                        HttpPost httpPost = new HttpPost(serverPath);                        //创建请求参数                        List<NameValuePair> parameters = new ArrayList<NameValuePair>();                        parameters.add(new BasicNameValuePair("username",username));                        parameters.add(new BasicNameValuePair("password",password));                        //请求参数设置到httpPost的实体里                        httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));                        //浏览器提交这个请求,然后服务器返回一个HttpResponse                        HttpResponse httpResponse = httpClient.execute(httpPost);                        //通过返回信息获取返回的状态码                        int statusCode = httpResponse.getStatusLine().getStatusCode();                        if (200 == statusCode) {                            InputStream inputStream = httpResponse.getEntity().getContent();                            final String responseMsg = StreamTool.getString(inputStream);                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_LONG).show();                                }                            });                        } else {                            System.out.println("statusCode = " + statusCode);                            //连接服务器出错,错误代码为:responseCode 根据代码值告诉用户出错的原因                            //....                        }                    } catch (Exception e) {                        e.printStackTrace();                    }                }            }).start();        }    }

更多相关文章

  1. 在Android(OPhone)模拟器中加载和使用SDCard卡
  2. Android调用手机浏览器
  3. 在Android Studio中,如何在app中打开链接网页,并且不会再跳转出浏
  4. ubuntu下启动AS 模拟器报错" Unknown AVD name "
  5. Android SD 卡文件浏览器
  6. android studio 61歌曲服务器搭建 歌曲app 下载 完整代码
  7. Android调用mongoose实现Web服务器

随机推荐

  1. Android C++ 动态加载so
  2. android中动画的学习
  3. FlexboxLayout使用指南
  4. android修改系统时间
  5. SharedPreferences 源码分析
  6. iphone 开发 精华
  7. android亮屏、暗屏、解锁、关闭系统对话
  8. Android(安卓)桌面Widget (小组件)开发详
  9. Android获取屏幕尺寸
  10. Android中开关按钮IOS效果的实现