首页 > 解决方案 > Android retrofit expected BEING_OBJECT but was string at line 1 column 1

问题描述

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);
?>

标签: javaphpandroidapi

解决方案


您正在服务器响应中打印“connesso”。在你的 save.php 文件中删除它。

使用以下内容更新您在 save.php 中的查询:

$query="INSERT INTO `Utenti`(`username`, `password`) VALUES('".$username."','".$password')."');";

推荐阅读