public void login(View view)
{
String url = "http://10.0.2.2/myserver_war_exploded/userLogin.html";
String username= ((EditText)findViewById(R.id.username)).getText().toString();//获取用户名
String password = ((EditText)findViewById(R.id.password)).getText().toString();//获取密码
OkHttpClient client = new OkHttpClient();
//创建RequestBody对象,添加参数
RequestBody requestBody = new FormBody.Builder().add("username",username).add("password",password).build();
//创建post方法的request对象
Request request = new Request.Builder().post(requestBody).url(url).build();
client.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
if (e instanceof ConnectException) {
Log.e("frost_connection",e.getMessage());
System.out.println("fail1");
}
System.out.println("fail2");
}
@Override
public void onResponse(Call call, Response response) throws IOException
{
if(response.isSuccessful())
{
System.out.println("连接成功");
}
}
});
}
无论如何都是onfailure,在avd虚拟机中直接浏览器访问10.0.2.2也进不去,求懂android客户端和服务器端的大神解答。
后台服务器也是本机实现,昨天尝试时请求可以用服务器接受,今天不行了。服务器端没有问题,使用电脑浏览器可以访问。贴上昨天尝试成功的解决方案:
manifests添加了:
application标签中添加了:
android:usesCleartextTraffic="true"