我有一个登录界面,可以从用户那里获取用户名和密码,并在后端验证它。我到目前为止编写的代码是
package com.example.opinion;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;
public class Login extends Activity {
private Button login;
private Button reg;
private EditText uname;
private EditText pword;
public static final String BASE_URL = "http://192.168.0.105/";
public interface LoginApi{
@POST("login")
Call<HttpBinResponse> userLogin(@Body LoginData data);
}
static class HttpBinResponse {
String result;
}
public class LoginData {
String username;
String password;
public LoginData(String uname, String pword) {
this.username = uname;
this.password = pword;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login =(Button) findViewById(R.id.btnlogin);
reg =(Button) findViewById(R.id.btnregister);
uname = (EditText)findViewById(R.id.uname);
pword = (EditText)findViewById(R.id.pass);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = uname.getText().toString();
String pass = pword.getText().toString();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginData user = new LoginData(name, pass);
System.out.println(user.username+user.password);
LoginApi apiService = retrofit.create(LoginApi.class);
Call<HttpBinResponse> call = apiService.userLogin(user);
call.enqueue(new Callback<HttpBinResponse>() {
@Override
public void onResponse(Call<HttpBinResponse> call, Response<HttpBinResponse> response) {
int statusCode = response.code();
HttpBinResponse decodedResponse = response.body();
if (decodedResponse == null)
return;
// at this point the JSON body has been successfully parsed
System.out.println(decodedResponse.result);
}
@Override
public void onFailure(Call<HttpBinResponse> call, Throwable t) {
}
});
Intent it= new Intent(Login.this, Home.class);
startActivity(it);
}
});
reg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it = new Intent(Login.this, Registration.class);
startActivity(it);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
在上面的代码中,LoginData类用于附加我的帖子请求的正文。 HttpBinResponse类用于获取响应并将其存储在result属性中。但我检查了我的代码没有返回任何东西,因为decodingResponse.result是null。
看到这个:
05-30 11:35:59.186 31176-31194 /? I / OpenGLRenderer:初始化的EGL, 版本1.4 05-30 11:36:08.783 31176-31176 / com.example.opinion I / System.out:hkbffghjk 05-30 11:36:08.877 31176-31176 / com.example.opinion I / System.out:null
而不是null它应该是一个json对象,其中键作为结果及其值。 任何人都可以告诉我如何得到适当的回应。
似乎我正在创建的网址创建不正确,或者帖子请求的主体没有附加或其他一些问题。
我检查了我的服务器日志,发现请求已到达服务器,但之后没有任何反应。