Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
Php无法将JSON数组返回到Android应用程序_Php_Android_Json_Http - Fatal编程技术网

Php无法将JSON数组返回到Android应用程序

Php无法将JSON数组返回到Android应用程序,php,android,json,http,Php,Android,Json,Http,我正在尝试编写一个简单的应用程序,要求用户创建一个帐户。我决定使用mysql/php作为后端,使用Android创建实际的应用程序。用户可以从应用程序中输入用户名和密码,后端将其存储在数据库中。然后,一个json数组将返回到应用程序。不幸的是,我的代码不起作用。应用程序只是崩溃,日志上没有打印任何内容 以下是Php: $json = array(); $master = array(); $json['status'] = "fail"; $json['message'] = "unable t

我正在尝试编写一个简单的应用程序,要求用户创建一个帐户。我决定使用mysql/php作为后端,使用Android创建实际的应用程序。用户可以从应用程序中输入用户名和密码,后端将其存储在数据库中。然后,一个json数组将返回到应用程序。不幸的是,我的代码不起作用。应用程序只是崩溃,日志上没有打印任何内容

以下是Php:

$json = array();
$master = array();
$json['status'] = "fail";
$json['message'] = "unable to connect to the server";
$master['master'] = $json;
echo json_encode($master);
exit;
这是android代码。我正在使用AsyncTask类来进行联网。这是执行工作的功能:

    protected Void doInBackground(String... urls)
    {
        HttpPost httppost = new HttpPost(urls[0]);
        BufferedReader reader = null;

        try
        {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            nameValuePairs.add(new BasicNameValuePair("method", "create"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = Client.execute(httppost);

            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                // Append server response in string
                sb.append(line + "");
            }

             // Append Server Response To Content String lol
            Content = sb.toString();
        }
        catch(Exception ex)
        {
            Error = ex.getMessage();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch(Exception ex) 
            {

            }
        }

        return null;
    }
protectedvoiddoinbackground(字符串…URL)
{
HttpPost HttpPost=新的HttpPost(URL[0]);
BufferedReader reader=null;
尝试
{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
添加(新的BasicNameValuePair(“方法”,“创建”));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=Client.execute(httppost);
reader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuilder sb=新的StringBuilder();
字符串行=null;
//读取服务器响应
而((line=reader.readLine())!=null)
{
//在字符串中追加服务器响应
sb.追加(第+行“);
}
//将服务器响应附加到内容字符串lol
Content=sb.toString();
}
捕获(例外情况除外)
{
Error=ex.getMessage();
}
最后
{
尝试
{
reader.close();
}
捕获(例外情况除外)
{
}
}
返回null;
}
这种组合会导致崩溃。我已验证请求是否到达服务器。有人能提供一些见解吗?

试试看

header('Content-type: application/json');
echo json_encode($master);

“无法连接到服务器”来自服务器,****警方。。