private void sentRequestWithOkHttpToLogin() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client=new OkHttpClient();
RequestBody requestBody=new FormBody.Builder()
.add("username",username)
.add("password",password)
.build();
Request request=new Request.Builder()
.url(Login_url)
.post(requestBody)
.build();
Response response= client.newCall(request).execute();
responseData=response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(responseData.equals("success"))
{
Log.d("user12",responseData);
Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
UserFragment.login.setText(username);
}else if(responseData.equals("fail"))
{
Log.d("user12",responseData);
Toast.makeText(LoginActivity.this,"用户名密码不存在",Toast.LENGTH_SHORT).show();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
返回值最后多了换行符,用string.replaceAll("\r|\n","");试试
这个决定于responseData所返回的值吧,还有最好确认下responseData是不是定义为string类型的。
你这个请求只会返回“success”或者“fail”这一个字段吗?