I use WampServer for connect backEnd to my android project and I use PHP and MySQL.
When I test my request with postman is works correctly but in android body of response is null. my laptop and mobile are on the same network and response received by android but body is null.
i check response.code() and it's 403 forbidden;
I am new in android and PHP and I don't know how can I fix this problem.
This is my PHP code:
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if (isset($_POST['userName'])) {
$userName = $_POST['userName'];
$result = mysql_query("SELECT *FROM members WHERE userName = '$userName'");
if (!empty($result)) {
if (mysql_num_rows($result) > 0) {
$response["success"] = 0;
echo json_encode($response);
} else {
$response["success"] = 1;
echo json_encode($response);
}
} else {
$response["success"] = 1;
echo json_encode($response);
}
} else {
$response["success"] = 0;
echo json_encode($response);
}
?>
and this is my android api code:
@FormUrlEncoded
@POST("project/creataccountusername.php")
Call<createAccountJm> createacountusername(@Field("userName") String userName);
This is my android model:
public class createAccountJm {
int success;
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
}
public class CreatAccount extends AppCompatActivity {
private Button next;
private TextInputLayout error;
private TextInputEditText username;
private String name;
private SharedPreferences share;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creataccountpage);
next=findViewById(R.id.creataccount);
error=findViewById(R.id.usernameet);
username=findViewById(R.id.usernameinput);
share=getSharedPreferences("createAccount",MODE_PRIVATE);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.3.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
final apiInterface service = retrofit.create(apiInterface.class);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name=username.getText().toString();
Call<createAccountJm> call=service.createacountusername(name);
call.enqueue(new Callback<createAccountJm>() {
@Override
public void onResponse(Call<createAccountJm> call, Response<createAccountJm> response) {
if(response.body().getSuccess()==1){
SharedPreferences.Editor edit =share.edit();
edit.putString("username",name);
edit.commit();
Intent emailpage = new Intent(CreatAccount.this, enteremailpage.class);
startActivity(emailpage);
}
else{
error.setError("این نام کاربری در حال حاضر استفاده میشود!");
}
}
@Override
public void onFailure(Call<createAccountJm> call, Throwable t) {
error.setError("اشکال در ارتباط با سرور لطفا مجددا تلاش کنید!");
}
});
});
}
}
it is response in postman:
{
"success": 1
}