java - Android JSON转换问题

标签 java android mysql json

[这是无法转换的字符串] 和 1 [编辑:这是我的错误的 logcat] 2我目前正在尝试为我的应用程序实现登录,该应用程序具有登录、登录请求、注册和注册请求 Activity 。在我的注册 Activity 中,我将用户数据发送回我的数据库,但是当我尝试在我的设备上执行此操作时,我在 Android 监视器中收到错误消息: “org.json.JSONException:值

我使用 MySQL 和 000Webhost 作为我的数据库和托管。下面是我认为可能与错误有关的代码,但我不太确定。如果有人可以在这里提供帮助,我将不胜感激。

touristRegisterBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String touristName = touristNameRegET.getText().toString();
            final String touristEmail = touristEmailRegET.getText().toString();
            final String touristPassword = touristPasswordRegET.getText().toString();


            Response.Listener<String> responseListener = new Response.Listener<String>(){

                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if(success){
                            Intent intent = new Intent(touristRegister.this, touristLogin.class);
                            touristRegister.this.startActivity(intent);

                        }else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(touristRegister.this);
                            builder.setMessage("Registration failed!")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }`

最佳答案

你的logcat显示<br><table of type java.lang.String cannot be converted to JSONObject这基本上意味着您尝试解析为 JSON 字符串的字符串不是 JSON 字符串。从外观上看,这是一个 HTML 文档。

基本上是response变量无效,无法转换。您要么指向了错误的 URL,要么服务器返回了错误的数据。如果您不确定 JSON 字符串应该是什么样子,这里有一个示例

{
    "username": "myusername",
    "token": "JD297H48FUO2J048JF729F2",
    "wadus": [1, 2, 3, 4],
    "timestamp": 123144535143,
    "success": true
}

关于java - Android JSON转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850750/

相关文章:

mysql - 在mariadb中查询数据时如何包含两个SET语句

c# - 为连接字符串动态提供用户名和密码

java - 如何正确地将引用从一个数组列表传输到另一个数组列表

java - Spring异常处理: the @exception handler is not invoked

android - 尝试在设备上绘制带纹理的三角形失败,但模拟器可以运行。为什么?

android - AlertDialog 不等待输入

java .matches() 不匹配

java - 让用户通过应用程序连接数据库的正确方法

android - MutableLiveData 中 setValue() 和 postValue() 的区别

php - 关于php和mysql