Php org.json.JSONException:Value<;br type java.lang.String无法转换为JSONArray

Php org.json.JSONException:Value<;br type java.lang.String无法转换为JSONArray,php,android,json,Php,Android,Json,我想创建一个简单的登录。 但我总是得到一个“org.json.JSONException:值类型java.lang.String不能转换为JSONArray错误(阅读标题了解完整错误) 日志: 11-21 12:33:34.424 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection 11-21 12:33:37.158 2427-2427

我想创建一个简单的登录。 但我总是得到一个
“org.json.JSONException:值类型java.lang.String不能转换为JSONArray错误
(阅读标题了解完整错误)

日志:

 11-21 12:33:34.424 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:37.158 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:37.158 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:40.420 2427-2681/package+appname D/NetworkSecurityConfig: No Network Security Config specified, using platform default
我测试了我对php文件的响应,结果是:
[{“code”:“login_success”,“id”:“3”,“name”:“Karl”,“password”:“qaz”,“email”:“iop”}]

在调试期间,我看到他在“
AppController.getInstance().addToRequestQueue(req)”之后出现了一些问题“但是我不符合我们使用的
AppController
类。我的问题是,我缺少什么?非常感谢您的帮助

我的
LoginRegister
Acitvity:

public class LoginRegister extends AppCompatActivity {

final String TAG = this.getClass().getSimpleName();
Button login;
EditText email, pw;
String DataEmail, DataPw;
TextView tvE, tvP;
String ResponseID, ResponseName, ResponsePW, ResponseEMail;

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

    login = (Button) findViewById(R.id.btnLogin);

    email = (EditText) findViewById(R.id.etEmail);
    pw = (EditText) findViewById(R.id.etPassword);

    tvE = (TextView) findViewById(R.id.tvName);
    tvP = (TextView) findViewById(R.id.tvPW);
 }

public void main_register(View v) {
    onPause();
    startActivity(new Intent(this, Register.class));
}

public void loginClicked(View v) {
    DataEmail = email.getText().toString();
    DataPw = pw.getText().toString();

    String url = "http://10.0.2.2/getinfoCAF.php";
    //String url = "http://10.0.3.2/getinfoCAF.php";
    //String url = "http://192.168.1.128/getinfoCAF.php";
    makeJsonArrayRequest(url);
}

private void makeJsonArrayRequest(String url)
{
    JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>()
    {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());

            try {
                String jsonResponse = "";
                for (int i = 0; i < response.length(); i++) {
                    JSONObject person = (JSONObject) response.get(i);
                    String success = person.getString("code");
                    String id = person.getString("id");
                    String name = person.getString("name");
                    String pw = person.getString("password");
                    String email = person.getString("email");

                    jsonResponse += "Success: " + success + "\n\n";
                    jsonResponse += "Name: " + name + "\n\n";
                    jsonResponse += "Email: " + email + "\n\n";
                    jsonResponse += "ID: " + id + "\n\n";
                    jsonResponse += "Mobile: " + pw + "\n\n\n";
                }

                tvE.setText(jsonResponse);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(),   Toast.LENGTH_LONG).show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("email", DataEmail);
            hashMap.put("password", DataPw);

            return hashMap;
        }
        /*
        protected Map<String, String> getParams()
        {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("email", DataEmail);
            hashMap.put("password", DataPw);

            return hashMap;
        }
        */
    };
    AppController.getInstance().addToRequestQueue(req);
}}
PHP代码片段:
`

My Manifest.xml

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  <application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginRegister">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Register">

    </activity>

 </application>

我不知道我做错了什么或错过了什么

编辑 问题是,服务器在EditTextfields中的参数typedin为null

解决方案

@Override
public Map<String, String> getParams() 
{
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("email", DataEmail);
        hashMap.put("password", DataPw);

        return hashMap;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError 
{
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("Content-Type", "application/x-www-form-urlencoded");

    return hashMap;
}
@覆盖
公共映射getParams()
{
HashMap HashMap=新的HashMap();
hashMap.put(“电子邮件”,DataEmail);
hashMap.put(“密码”,DataPw);
返回hashMap;
}
@凌驾
公共映射getHeaders()引发AuthFailureError
{
HashMap HashMap=新的HashMap();
put(“内容类型”、“应用程序/x-www-form-urlencoded”);
返回hashMap;
}

我需要两个哈希图,第一个是数据,第二个是我要发送的额外信息。

通过检查logcat,您的
EditText
TextView
似乎有问题。检查类似的StackOverflow线程。
AppController
是一个单例类。这里它用于截击网络请求。看看您是否有问题pe
http://10.0.2.2/getinfoCAF.php
在你的浏览器中,如果返回的是
JSON
你的服务器响应是正常的,但是如果返回的是HTML页面,那就是这里的罪魁祸首。因为在Android部分,你实际上期待的是JSON。如果我输入了正确的电子邮件,但是我没有得到HTML,只要[{“code”:“登录成功”,“id”“:”3“,”姓名“:”卡尔“,”密码“:”qaz“,”电子邮件“:”iop“}]。但是我在日志中看不到我的回复…即使我打错了电子邮件或者我收到了json回复。但是仍然是“D/Volley:[1]2.onErrorResponse:LoginRegister”(线程标题错误),您在日志猫中看不到您的响应,因为它永远不会进入onResponse回调。尝试将JSONArrayRequest更改为StringRequest,以便查看返回的数据。通过检查logcat,您的
EditText
TextView
似乎有问题。检查类似的StackOverflow线程和
AppController
是一个单例类。这里它用于截击网络请求。查看是否键入
http://10.0.2.2/getinfoCAF.php
在您的浏览器中,如果返回的是
JSON
,则服务器响应正常,但如果返回的是任何HTML页面,则这就是罪魁祸首。因为在Android部分,你实际上需要JSON。如果我输入了正确的电子邮件,我没有收到html,只要[{“code”:“login_success”,“id”:“3”,“name”:“Karl”,“password”:“qaz”,“email”:“iop”}]。但是我在日志中看不到我的回复…即使我打错了电子邮件或者我收到了json回复。但是仍然是“D/Volley:[1]2.onErrorResponse:LoginRegister”(线程标题错误),您在日志猫中看不到您的响应,因为它永远不会进入onResponse回调。尝试将JSONArrayRequest更改为StringRequest,以便查看要返回的数据。
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  <application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginRegister">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Register">

    </activity>

 </application>
@Override
public Map<String, String> getParams() 
{
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("email", DataEmail);
        hashMap.put("password", DataPw);

        return hashMap;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError 
{
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("Content-Type", "application/x-www-form-urlencoded");

    return hashMap;
}