预期的begin_array但在行处是字符串

时间:2019-04-29 12:24:54

标签: android arrays retrofit

我想学习改装,但我无法解决此错误 我的应用程序必须从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代码,但在这里我们仅从现在开始使用respans,就不会从数据库中获取任何数据

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

1 个答案:

答案 0 :(得分:0)

请发布您的JSON响应,以便我们提供更多帮助。当解析您的API的代码期望在JSON数组中列出一个列表,而Retrofit发现一个String导致异常,该异常导致Retrofit无法完成解析过程时,会发生此错误。

换句话说,您期望得到这样的答复:

{
  [
    { .. }
  ]
}

但是您却收到了:

{
  "SOME STRING"
}

因此,您有两个选项可以解决此问题,您可以更改API以返回列表以匹配期望的格式,或者更改Retrofit响应对象以匹配从API实际收到的字符串。 / p>