java - 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);
?>
解决方案
您正在服务器响应中打印“connesso”。在你的 save.php 文件中删除它。
使用以下内容更新您在 save.php 中的查询:
$query="INSERT INTO `Utenti`(`username`, `password`) VALUES('".$username."','".$password')."');";
推荐阅读
- java - 是否可以读取传递 mbox 数据而不是路径的“mbox”文件?
- html - 在 Visual Studio 2019 中缩小
- c# - 当我测试检查它们时,为什么未更改的 DatagridViewCheckboxCells 会抛出空异常?
- macos - 如何通过终端或 M1 MacBook 上的 Apple API 获取 CPU 温度或风扇速度?
- r - RDCOMClient Outlook 在 R 中添加提醒
- node.js - “http”模块的“IncomingMessage”中未定义的“url”属性是什么意思?
- android - 如何使用资源字符串与占位符连接?
- ruby-on-rails - 我怎样才能只查询graphql中的必填字段?
- javascript - 对象中定义的函数对此有不同的实现,但普通函数有不同的原因?
- node.js - Typescript:使用 require 和 import 同步/异步