简体   繁体   English

java.lang.string 类型的连接值无法转换为 jsonobject [Android Volley Library]

[英]Value connected of type java.lang.string cannot be converted to jsonobject [Android Volley Library]

I know, i know, it's an old question to ask but I'm totally stuck.我知道,我知道,这是一个老问题,但我完全被困住了。 I've been trying to solve it using all those answer on Stackoverflow but still helpless.我一直在尝试使用 Stackoverflow 上的所有这些答案来解决它,但仍然无能为力。 So, this is my code:所以,这是我的代码:

[Java]: [爪哇]:

package com.projectbengkelin;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {

     EditText Edtnama,Edtemail,Edtalamat,EdtnoHp,Edtpassword;
     Button btnLanjut;
     Button btnKembali;
     ProgressBar loading;
     String nama, email, alamat, noHp, password;
    //private static String URL_REGISTER = "http://192.168.56.1/finals-mobile/register.php";
     String URL_REGISTER = "https://bengkelinteam.000webhostapp.com/api/insert_user.php";

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

        Edtnama = findViewById(R.id.nama);
        Edtemail = findViewById(R.id.email);
        Edtalamat = findViewById(R.id.alamat);
        EdtnoHp = findViewById(R.id.noHp);
        Edtpassword = findViewById(R.id.password);
        loading = findViewById(R.id.loading);
        btnLanjut = findViewById(R.id.btnLanjut);
        btnKembali = findViewById(R.id.btnKembali);

        btnKembali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openMainActivity();
            }
        });

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


    private void Register() {
        loading.setVisibility(View.VISIBLE);
        btnLanjut.setVisibility(View.GONE);

         nama = Edtnama.getText().toString();
         email = Edtemail.getText().toString();
         alamat = Edtalamat.getText().toString();
         noHp = EdtnoHp.getText().toString();
         password = Edtpassword.getText().toString();
         RequestQueue requestQueue = Volley.newRequestQueue(Register.this);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGISTER, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int sukses = jsonObject.getInt("success");
                            // Toast.makeText(Register.this, "Setelah jsonObject", Toast.LENGTH_SHORT).show();
                            if (sukses == 1) {
                                Toast.makeText(Register.this, "Register Success!", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(Register.this, "Register Failed!" + e.toString(), Toast.LENGTH_LONG).show();
                            loading.setVisibility(View.GONE);
                            btnLanjut.setVisibility(View.VISIBLE);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Register.this, "Register Failed!" + error.toString(), Toast.LENGTH_SHORT).show();
                        //error.printStackTrace();
                        Log.e("Error", error.getMessage());
                        loading.setVisibility(View.GONE);
                        btnLanjut.setVisibility(View.VISIBLE);
                    }
                })
        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("nama", nama);
                params.put("email", email);
                params.put("alamat", alamat);
                params.put("noHp", noHp);
                params.put("password", password);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        //RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.getCache().clear();
        requestQueue.add(stringRequest);
    }
    public void openMainActivity(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

And then, here is my PHP code which is I'd put on 000webhost:然后,这是我在 000webhost 上的 PHP 代码:

[PHP]: [PHP]:

<?php
header('Content-type:application/json;chartset=utf-8');
include 'conn.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $noHp = $_POST['noHp'];
    $email = $_POST['email'];
    $nama = $_POST['nama'];
    $alamat = $_POST['alamat'];
    $password = $_POST['password'];

   // $password = password_hash($password, PASSWORD_DEFAULT);

   // require_once 'connect.php';

    $q = mysqli_query($conn, "INSERT INTO user (noHp, email ,nama, alamat, password) VALUES ('$nohp','$email','$nama','$alamat','$password')");

    if ($q) {
        $response["success"] = "1";
        $response["message"] = "success";
        echo json_encode($response);
        mysqli_close($conn);

    } else {
        $result["success"] = "0";
        $result["message"] = "error";
        echo json_encode($response);
        mysqli_close($conn);
    }
}

Everytime I tried to insert the data using that API JSON, I always get this error messange:每次我尝试使用 API JSON 插入数据时,我总是收到以下错误消息:

value connected of type java.lang.string cannot be converted to jsonobject

Please help me.请帮我。

there is simple issue in your php code - do like that:您的 php 代码中有一个简单的问题 - 这样做:

     $response=array();

     $q = mysqli_query($conn, "INSERT INTO user (noHp, email ,nama, alamat, password) VALUES ('$nohp','$email','$nama','$alamat','$password')");

        if ($q) {
            $response["success"] = "1";
            $response["message"] = "success";
        } else {
            $response["success"] = "0";
            $response["message"] = "error";
        }
     echo json_encode($response);
     mysqli_close($conn);

use Postman to check your response使用Postman检查您的回复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Volley库W / System.err:org.json.JSONException:类型为java.lang.String的Value Connected无法转换为JSONObject - Volley library W/System.err: org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject java.lang.String 类型的值 Connected 无法转换为 JSONObject - Value Connected of type java.lang.String cannot be converted to JSONObject java.lang.String 类型的值无法转换为 JSONObject (Volley Android Php API) - Value of type java.lang.String cannot be converted to JSONObject ( Volley Android Php API) 值[{类型为java.lang.String的值无法在android中转换为JSONObject - Value [{ of type java.lang.String cannot be converted to JSONObject in android com.android.volley.parseerror org.json.json异常值类型为java.lang.String的无法转换为JSONObject - com.android.volley.parseerror org.json.jsonexception value yes of type java.lang.String cannot be converted to JSONObject java.lang.string类型的Java值无法转换为jsonobject - Java value of type java.lang.string cannot be converted to jsonobject 值 - Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 类型为java.lang.String的值yes无法转换为JSONObject - Value yes of type java.lang.String cannot be converted to JSONObject java.lang.String类型的值数据库无法转换为JSONObject - Value Database of type java.lang.String cannot be converted to JSONObject java.lang.String 类型的值数组无法转换为 JSONObject - Value Array of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM