英文:
Android retrofit expected BEING_OBJECT but was string at line 1 column 1
问题
// Api Client
public class ApiClient {
private static final String BASE_URL = "https://tommytest22.000webhostapp.com/";
private static Retrofit retrofit;
public static Retrofit getApiClient() {
if (retrofit == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
// Api Interface
public interface ApiInterface {
@FormUrlEncoded
@POST("/save.php")
Call<Utente> salvaUtente(
@Field("username") String username,
@Field("password") String password
);
}
// User Class
public class Utente {
@Expose
@SerializedName("id_utente") private int id_utente;
@Expose
@SerializedName("username") private String username;
@Expose
@SerializedName("password") private String password;
@Expose
@SerializedName("success") private Boolean success;
@Expose
@SerializedName("messaggio") private String messaggio;
}
// Registration Method activate by click on button
private boolean registraUtente(final String username, final String password){
progressDialog.show();
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<Utente> call = apiInterface.salvaUtente(username, password);
call.enqueue(new Callback<Utente>() {
@Override
public void onResponse(@NonNull Call<Utente> call, @NonNull Response<Utente> response) {
progressDialog.dismiss();
if (response.isSuccessful() && response.body() != null) {
Boolean success = response.body().getSuccess();
if (success) {
Toast.makeText(MainActivity.this, response.body().getMessaggio(), Toast.LENGTH_LONG).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, response.body().getMessaggio(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull Call<Utente> call, @NonNull Throwable t) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
return true;
}
// connect.php
<?php
$conn = mysqli_connect(PARAMETERS);
if ($conn) {
} else {
echo "not connected";
}
?>
// save.php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
require_once("connect.php");
$query = "INSERT INTO `Utenti`(`username`, `password`) VALUES('$username','$password')";
if (mysqli_query($conn, $query)) {
$response['success'] = true;
$response['message'] = "Utente inserito correttamente";
} else {
$response['success'] = false;
$response['message'] = "Utente non inserito";
}
} else {
$response['success'] = false;
$response['message'] = "CONNESSIONE NON RIUSCITA";
}
echo json_encode($response);
?>
英文:
I'm having trouble while i was creating an Android App that have to LogIn or Register. I'm using Retrofit that make a request to a php file in my server. Below I put my code, i searched everywhere for a solution but I didn't found nothing. What I'm tryng to do is registration for now, I'm tryng to insert data in my sql database. Sorry for my bad english
Api Client
public class ApiClient {
private static final String BASE_URL = "https://tommytest22.000webhostapp.com/";
private static Retrofit retrofit;
public static Retrofit getApiClient() {
if (retrofit == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
Api Interface
public interface ApiInterface {
@FormUrlEncoded
@POST("/save.php")
Call<Utente> salvaUtente(
// @Field("id_utente") int id_utente,
@Field("username") String username,
@Field("password") String password
);
}
User Class
public class Utente {
@Expose
@SerializedName("id_utente") private int id_utente;
@Expose
@SerializedName("username") private String username;
@Expose
@SerializedName("password") private String password;
@Expose
@SerializedName("success") private Boolean success;
@Expose
@SerializedName("messaggio") private String messaggio;
}
Registration Method activate by click on button
private boolean registraUtente(final String username,final String password){
progressDialog.show();
apiInterface= ApiClient.getApiClient().create(ApiInterface.class);
Call<Utente> call= apiInterface.salvaUtente(username,password);
call.enqueue(new Callback<Utente>() {
@Override
public void onResponse(@NonNull Call<Utente> call, @NonNull Response<Utente> response) {
progressDialog.dismiss();
if (response.isSuccessful() && response.body() != null) {
Boolean success = response.body().getSuccess();
if (success) {
Toast.makeText(MainActivity.this, response.body().getMessaggio(), Toast.LENGTH_LONG).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, response.body().getMessaggio(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull Call<Utente> call, @NonNull Throwable t) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
return true;
}
connect.php
<?php
$conn=mysqli_connect(PARAMETERS);
if($conn){
}else{
echo "not connected";
}
?>
save.php:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$username=$_POST['username'];
$password=$_POST['password'];
require_once("connect.php");
$query="INSERT INTO `Utenti`(`username`, `password`) VALUES('$username','$password')";
if(mysqli_query($conn,$query)){
$response['success']=true;
$response['message']="Utente inserito correttamente";
}else{
$response['success']=false;
$response['message']="Utente non inserito ";
}
}else{
$response['success']=false;
$response['message']="CONNESSIONE NON RIUSCITA ";
}
echo json_encode($response);
?>
答案1
得分: 0
你的服务器响应中打印了“connesso”。请在你的save.php文件中删除这部分。
使用以下内容更新你在save.php中的查询:
$query = "INSERT INTO `Utenti`(`username`, `password`) VALUES('{$username}','{$password}')";
英文:
You are printing "connesso" in your server response. Remove this in your save.php file.
Update your query in save.php with this:
$query="INSERT INTO `Utenti`(`username`, `password`) VALUES('".$username."','".$password')."');";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论