Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用json与php mysql连接的android_Php_Android_Mysql_Json - Fatal编程技术网

使用json与php mysql连接的android

使用json与php mysql连接的android,php,android,mysql,json,Php,Android,Mysql,Json,我已经在android系统上创建了一个登录活动,需要与php和mysql连接,它工作得很好,但现在我需要转换我的代码以获得JSON响应,但我不知道如何更改php和java中的代码以使我的应用程序适用于JSON 如果有人能帮助我,我将不胜感激 这是我的密码 check.php 尝试使用此响应response=httpclient.execute(httppost)从代码中,将其序列化为JSON,如下所示string responseText=EntityUtils.toString(respons

我已经在android系统上创建了一个登录活动,需要与php和mysql连接,它工作得很好,但现在我需要转换我的代码以获得JSON响应,但我不知道如何更改php和java中的代码以使我的应用程序适用于JSON

如果有人能帮助我,我将不胜感激 这是我的密码

check.php
尝试使用此响应
response=httpclient.execute(httppost)
从代码中,将其序列化为JSON,如下所示
string responseText=EntityUtils.toString(response.getEntity());
JSONObject json=新的JSONObject(responseText)

这将把响应放入对象
json

将if()语句
if(response.equalsIgnoreCase(“User Found”)
更改为
if(json.has(“User Found”)

您可能还需要添加缺少的导入


希望这有帮助。

在php端,您必须使用函数json_encode()对返回的数据进行编码。请检查此处的文档

例如:

header('Content-type: application/json');
echo json_encode(array('response'=>'user_found'));
在响应的Java/Android端,您必须使用JSONObject,如下所示:

// Instantiate a JSON object from the request response
    JSONObject jsonObject = new JSONObject(response);
然后,当您在JSONObject中拥有数据时,您可以检查文档以根据需要使用它。

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;

public class UserPage extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userpage);

    }
}
header('Content-type: application/json');
echo json_encode(array('response'=>'user_found'));
// Instantiate a JSON object from the request response
    JSONObject jsonObject = new JSONObject(response);