首先,我知道这个问题已经被问过很多遍了,但是我还没有解决。我在应用程序中使用Retrofit,我想在其中向服务器发送3个变量。不会调用onResponse方法,请检查我下面的代码并建议我该代码有什么问题,谢谢。
onResponse方法
private void loginByServer() {
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
String str = "customers";
Log.e("email______",email);
Log.e("password______",password);
Log.e("str______",str);
APIService service = ApiClient.getClient().create(APIService.class);
Call<String> userCall = service.userLogIn(email,password,str);
userCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
Log.e("Inner Part__________","_________________");
Log.e("Response_________", String.valueOf(response));
String string = response.body();
Log.e("Response body_________", string);
if(response.message().toString().equalsIgnoreCase("ok"));{
// progressDoalog.setVisibility(View.GONE);
Log.e("Successfully","Loged In__________");
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
System.out.println("failed to upload the file");
// progressDoalog.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
API客户端
public class ApiClient {
public static final String BASE_URL = "http://192.168.2.59/easyshop/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
API服务接口
public interface APIService {
@FormUrlEncoded
@POST("login.php")
Call<String> userLogIn(@Field("email") String email,
@Field("password") String password,
@Field("who") String who);
}
用于登录用户的php文件
<?php
include("db_config.php");
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_REQUEST['who'])) {
$username = $_REQUEST['email'];
$password = md5($_REQUEST['password']);
$who = $_REQUEST['who'];
if ($who == "customers") {
$sql = "SELECT COUNT(*) AS count FROM customers WHERE (Username = '".$username."' or Email = '".$username."') and Password = '".$password."'";
} else if($who == "retailers") {
$sql = "SELECT COUNT(*) AS count FROM retailers WHERE (Username = '".$username."' or Email = '".$username."') and Password = '".$password."'";
}
$result = mysqli_query($con,$sql);
$check = mysqli_fetch_array($result);
if ($check['count'] > 0) {
echo "Success";
} else {
echo "Failed";
}
}
?>
答案 0 :(得分:0)
缺少的部分是@POST(“ login.php”)上的/
public interface APIService {
@FormUrlEncoded
@POST("/login.php")
Call<String> userLogIn(@Field("email") String email,
@Field("password") String password,
@Field("who") String who);
}