java.lang.IllegalStateException:预期为BEGIN_OBJECT但是为STRING

时间:2016-02-20 05:45:40

标签: android retrofit illegalstateexception retrofit2

我已经写了一个Login功能并且与<{1}}进行沟通我正在使用改造,但总是“失败”< /强>

获得:

web service

LoginAPI.java:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

LoginActivity.java:

public interface LoginApi {
    @FormUrlEncoded
    @POST("/retrofit_user/login.php")
    public void getlogin(
        @Field("username") String username,
        @Field("password") String password,
        Callback<User> response);
}

User.java:

restAdapter = new RestAdapter.Builder().setEndpoint(ROOT_LOGIN).build();
restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
loginApi = restAdapter.create(LoginApi.class);

buttonlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

  loginApi.getlogin(editTextUsername.getText().toString(),
  editTextPassword.getText().toString(), new Callback<User>() {

  @Override
  public void success(User s, Response response) {
     Toast.makeText(LoginActivity.this,"Logged In",Toast.LENGTH_LONG).show();

    if(s != null) {
       Log.d("name:", s.getName());
       Log.d("email:", s.getEmail());
       Log.d("id:", String.valueOf(s.getId()));
    }

  }

  @Override
  public void failure(RetrofitError error) {

       Log.d("error:", error.toString());
 Toast.makeText(LoginActivity.this,"Failed",Toast.LENGTH_LONG).show();
          }
      });
    }
});

日志

public class User {

    String name;
    String username;
    String password;
    String email;
    int id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

这是我的D/Retrofit: <--- HTTP 200 http://domain.info/retrofit_user/login.php (942ms) D/Retrofit: : HTTP/1.1 200 OK D/Retrofit: Connection: Keep-Alive D/Retrofit: Content-Type: text/html; charset=UTF-8 D/Retrofit: Date: Sat, 20 Feb 2016 06:00:39 GMT D/Retrofit: Keep-Alive: timeout=5, max=100 D/Retrofit: Server: Apache/2.4.16 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 D/Retrofit: Transfer-Encoding: chunked D/Retrofit: X-Android-Received-Millis: 1455948038590 D/Retrofit: X-Android-Response-Source: NETWORK 200 D/Retrofit: X-Android-Selected-Transport: http/1.1 D/Retrofit: X-Android-Sent-Millis: 1455948037988 D/Retrofit: X-Powered-By: PHP/5.6.14 D/Retrofit: Done D/Retrofit: <--- END HTTP (4-byte body) D/error:: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ D/GraphicBuffer: create handle(0x55bb2e98) (w:256, h:66, f:1) D/GraphicBuffer: close handle(0x55bb2e98) (w:256 h:66 f:1) 脚本:

login.php

1 个答案:

答案 0 :(得分:1)

问题是,改造期望JSON输出可以从User加入login.php类,但login.php给出的所有内容都是&#34; Not Done& #34;或者&#34;完成&#34;。

你必须以下列格式制作你的php脚本输出json,

{
    "name": "name",
    "username": "username",
    "password": "password",
    "email": "email",
    "id": id
}

你必须编辑php代码。它会是这样的,

<?php
    $objConnect = mysql_connect("localhost","username","password");
    $objDB = mysql_select_db("database");

    /*** for sample */
    // $_POST["username"] = "sun";
    // $_POST["password"] = "live";

    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    $strSQL = "select * from test_users where username = '".$username."' and  password = '".$password."' ";

    $objQuery = mysql_query($strSQL);
    $intNumRows = mysql_num_rows($objQuery);
    if($intNumRows==0)
    {
        $response["success"] = 0;
        $response["name"] = "";
        $response["username"] = "";
        $response["password"] = "";
        $response["email"] = "";
        $response["id"] = 0;
    }
    else
    {
        $row = mysql_fetch_array($objQuery);
        $response["success"] = 1;
        $response["name"] = $row["name"];
        $response["username"] = $row["username"];
        $response["password"] = $row["password"];
        $response["email"] = $row["email"];
        $response["id"] = $row["id"];
    }

    mysql_close($objConnect);

    echo json_encode($response, JSON_NUMERIC_CHECK);
?>

还向int success;类添加User字段,并使用它来验证输出。所以你的成功方法将是,

@Override
public void success(User s, Response response) {
    if(s != null && s.getSuccess() == 1) {
        Toast.makeText(LoginActivity.this,"Logged In",Toast.LENGTH_LONG).show();
        Log.d("name:", s.getName());
        Log.d("email:", s.getEmail());
        Log.d("id:", String.valueOf(s.getId()));
    } else {
        Toast.makeText(LoginActivity.this,"Invalid!",Toast.LENGTH_LONG).show();
    }

}

<强>更新

id字段是数据库中的int,它将id作为字符串返回。请参阅this主题。在php中将JSON_NUMERIC_CHECK添加到json_encode()会有所帮助。