W_sJ1111 2021-08-21 18:52 采纳率: 14.3%
浏览 741

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:

在android客户端进行登录,输入账号密码,访问服务器端的servlet,连接数据库mysql查询是否有该用户,有就返回json数据给android,用Toast弹出登录成功。
json我是在服务端和客户端都封装了一个类Result,里面两个值一个String Rs和int value。运行的时候报错了,网上搜说是后台返回的数据不是纯json,帮忙看看我这样要怎么解决,谢谢!
客户端:

java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private String url = "http://192.168.1.102:8089/Contacts/LoginAndroidServlet";
    private String LoginAddress = null;

    private EditText tell;
    private EditText password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tell = findViewById(R.id.edit_inputTell);
        password = findViewById(R.id.edit_inputPwd);
        btn_login = findViewById(R.id.button_login);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_login){
            String tellnum = tell.getText().toString();
            String pwd = password.getText().toString();
            LoginAddress = url+"?tell="+tellnum+"&pwd="+pwd;
            login();
        }
    }

    private void login() {
        HttpUtil.sendOkHttpRequest(LoginAddress, new okhttp3.Callback(){

            @Override
            public void onFailure(Call call, IOException e) {
                //对异常情况进行处理
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //得到服务器返回的具体内容---获取到了LoginAndroidServlet用户登录的结果
                String responseData = response.body().string().trim();
                int result = parseJSONWithGson(responseData);
                if (result > 0){
                    //登录成功...待开发,转入新界面
                    Toast.makeText(MainActivity.this, "登录成功...待开发,转入新界面", Toast.LENGTH_SHORT).show();
                }else if (result < 0){
                    Toast.makeText(MainActivity.this, "服务器异常,等待修复", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "手机号或者密码错误,请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private int parseJSONWithGson(String jsonData){
        Gson gson = new Gson();
        Result rs = gson.fromJson(jsonData, Result.class);
        return rs.getValue();
    }


}

Result类

public class Result {

    private String Rs;

    private int value;  

    public Result() {

    }

    public Result(String rs, int value) {
        super();
        Rs = rs;
        this.value = value;
    }

    public String getRs() {
        return Rs;
    }

    public void setRs(String rs) {
        Rs = rs;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

服务器端:

package org.login.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.login.eneity.Login;
import org.login.eneity.Result;
import org.login.service.ILoginService;
import org.login.service.impl.LoginServiceImpl;

import com.google.gson.Gson;


public class LoginAndroidServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取输入信息
        String tell = request.getParameter("tell");
        String pwd = request.getParameter("pwd");
        System.out.println(tell + "服务器是否接收到android");
        System.out.println(pwd + "服务器是否接收到android");
        //获取了以后1.查询是否有此用户
        Login login = new Login(tell, pwd);
        ILoginService loginService =  new LoginServiceImpl();
        int result = loginService.login(login);
        response.setContentType("text/html; charset=utf-8");
        response.setCharacterEncoding("utf-8");    
        //result=0账号或密码有误,result=1成功, result=-1服务器异常
        if(result > 0) {
            /*传result的值给android
             * 方案1:查询出uid一并给android
             * 方案2:查询出该用户的联系人一并传给android
            Gson gson = new Gson();
            String json = gson.toJson(login);    */
            Result rs = new Result("result", 1);
            Gson gson = new Gson();
            String json_result = gson.toJson(rs);
            System.out.println(json_result);
            System.out.println(result);
            request.setAttribute("json_result", json_result);
            
        }else if(result < 0) {
            //传result的值给android
            Result rs = new Result("result", -1);
            Gson gson = new Gson();
            String json_result = gson.toJson(rs);
            System.out.println(json_result);
            request.setAttribute("json_result", json_result);    //服务器异常
        }else {
            //传result的值给android
            Result rs = new Result("result", 0);
            Gson gson = new Gson();
            String json_result = gson.toJson(rs);
            System.out.println(json_result);
            request.setAttribute("json_result", json_result);    //账号或密码错误
        }    
        request.getRequestDispatcher("login_a.jsp").forward(request, response);
        
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        doGet(request, response);
    }

}

转到的login_a.jsp,原来的想法是把json字符串在这个jsp页面输出,然后Android请求访问最后访问到这个页面就取到了result的值,来判断是否登录成功

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String json = request.getAttribute("json_result").toString().trim();
        if (json != null){
            out.print(json);
            System.out.print(json);
        }
    %>
</body>
</html>


谢谢

  • 写回答

2条回答 默认 最新

  • W_sJ1111 2021-08-21 19:13
    关注

    完整的报错忘记贴了

    E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
        Process: com.wsj.mail_list, PID: 21430
        com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    
    评论

报告相同问题?

问题事件

  • 创建了问题 8月21日

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?