Android的Retrofit预期是BEING_OBJECT,但实际在第1行第1列却是字符串。

huangapple go评论89阅读模式
英文:

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 = &quot;https://tommytest22.000webhostapp.com/&quot;;
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(&quot;/save.php&quot;)
Call&lt;Utente&gt; salvaUtente(
//  @Field(&quot;id_utente&quot;) int id_utente,
@Field(&quot;username&quot;) String username,
@Field(&quot;password&quot;) String password
);
}

User Class

public class Utente {
@Expose
@SerializedName(&quot;id_utente&quot;) private int id_utente;
@Expose
@SerializedName(&quot;username&quot;) private String username;
@Expose
@SerializedName(&quot;password&quot;) private String password;
@Expose
@SerializedName(&quot;success&quot;) private Boolean success;
@Expose
@SerializedName(&quot;messaggio&quot;) 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&lt;Utente&gt; call= apiInterface.salvaUtente(username,password);
call.enqueue(new Callback&lt;Utente&gt;() {
@Override
public void onResponse(@NonNull Call&lt;Utente&gt; call, @NonNull Response&lt;Utente&gt; response) {
progressDialog.dismiss();
if (response.isSuccessful() &amp;&amp; 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&lt;Utente&gt; call, @NonNull Throwable t) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
return true;
}

connect.php

&lt;?php   
$conn=mysqli_connect(PARAMETERS);
if($conn){
}else{
echo &quot;not connected&quot;;
}
?&gt;

save.php:

&lt;?php
if($_SERVER[&#39;REQUEST_METHOD&#39;]==&#39;POST&#39;){
$username=$_POST[&#39;username&#39;];
$password=$_POST[&#39;password&#39;];
require_once(&quot;connect.php&quot;);
$query=&quot;INSERT INTO `Utenti`(`username`, `password`) VALUES(&#39;$username&#39;,&#39;$password&#39;)&quot;;
if(mysqli_query($conn,$query)){
$response[&#39;success&#39;]=true;
$response[&#39;message&#39;]=&quot;Utente inserito correttamente&quot;;
}else{
$response[&#39;success&#39;]=false;
$response[&#39;message&#39;]=&quot;Utente non inserito &quot;;
}
}else{
$response[&#39;success&#39;]=false;
$response[&#39;message&#39;]=&quot;CONNESSIONE NON RIUSCITA &quot;;
}
echo json_encode($response);
?&gt;

答案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=&quot;INSERT INTO `Utenti`(`username`, `password`) VALUES(&#39;&quot;.$username.&quot;&#39;,&#39;&quot;.$password&#39;).&quot;&#39;);&quot;;

huangapple
  • 本文由 发表于 2020年8月28日 23:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63637196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定