【问题标题】:expected begin_array but was string at line预期 begin_array 但在行是字符串
【发布时间】:2019-09-18 01:51:28
【问题描述】:

我想学习改造,但我无法解决这个错误 我的应用程序必须从 php 代码中获取 gson,但是在运行预期的 begin_array 时出现错误但是是字符串 我的代码

package com.example.rami_.retrofitapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    TextView nametxt, agetxt, phonetxt, emailtxt;
    Button retrieveBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nametxt = (TextView) findViewById(R.id.nametxt);
        agetxt = (TextView) findViewById(R.id.agetxt);
        phonetxt = (TextView) findViewById(R.id.phonetxt);
        emailtxt = (TextView) findViewById(R.id.emailtxt);

        retrieveBtn = (Button) findViewById(R.id.retrieveBtn);

        retrieveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fetchData();
            }
        });



    }


    private void fetchData() {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        Api api = retrofit.create(Api.class);

        Call<List<Details_Pojo>> call = api.getstatus();

        call.enqueue(new Callback<List<Details_Pojo>>() {
            @Override
            public void onResponse(Call<List<Details_Pojo>> call, Response<List<Details_Pojo>> response) {
                List<Details_Pojo> adslist = response.body();

                String name = adslist.get(0).getName();
                String age = adslist.get(0).getAge();
                String phone = adslist.get(0).getPhone();
                String email = adslist.get(0).getEmail();

                nametxt.setText(name);
                agetxt.setText(age);
                phonetxt.setText(phone);
                emailtxt.setText(email);

            }

            @Override
            public void onFailure(Call<List<Details_Pojo>> call, Throwable t) {

                Toast.makeText(MainActivity.this, ""+t.getMessage().toString(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}

模型类

package com.example.rami_.retrofitapp;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * Created by Android on 1/6/2018.
 */

public class Details_Pojo {

@SerializedName("Name")
private String Name;
@SerializedName("Age")
private String Age;
@SerializedName("Phone")
private String Phone;
@SerializedName("Email")
private String Email;

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getAge() {
    return Age;
}

public void setAge(String age) {
    Age = age;
}

public String getPhone() {
    return Phone;
}

public void setPhone(String phone) {
    Phone = phone;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

}

api >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

package com.example.rami_.retrofitapp;
import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

/**
 * Created by Android on 1/6/2018.
 */

public interface Api {

    String BASE_URL = "http://10.0.2.2/retrofitapp/";
    @GET("fetch_data.php")
    Call<List<Details_Pojo>> getstatus();

}

这个连接数据库的php代码,但是这里我们没有从数据库中获取任何数据,我们现在只使用respoans

<?php

 require "init.php";
 $name = $_GET["Name"];
 $age = $_GET["Age"];
 $phone = $_GET["Phone"];
 $email = $_GET["Email"];

 $sql = "select * from login_info where name ='$name'";

 $result = mysqli_query($con, $sql);

 if(mysqli_num_rows($result) > 0)
 {
     $status ="exist";
 }
 else
 {
    $sql = "insert into login_info(name, age, phone, email) VALUES ('$name', '$age', '$phone',$email)";
    if (mysqli_query($con, $sql))
    {
        $status ="ok";
    }
    else
    {

        $status ="error";
    }
}
$response = array('Name' => "Rami",'Age' => "24",'Phone' =>
 "01004562638",'Email' => "24");                  

 echo json_encode($response);
     mysqli_close($con);
?>

【问题讨论】:

标签: android arrays retrofit


【解决方案1】:

请发布您的 JSON 回复,以便我们提供更多帮助。当解析 API 的代码需要 JSON 数组中的列表,但 Retrofit 发现了一个导致异常阻止 Retrofit 完成解析过程的字符串时,就会发生此错误。

换句话说,您期望这是响应:

{
  [
    { .. }
  ]
}

但你却收到了这个:

{
  "SOME STRING"
}

因此,您有两种选择来解决此问题,要么更改 API 以返回列表以匹配您期望的格式,要么更改 Retrofit 响应对象以匹配从 API 实际收到的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 2020-03-14
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2023-03-08
    相关资源
    最近更新 更多