首页 文章

PHP json_encode()什么都不返回

提问于
浏览
0

好的,前几天我用Java编写了一个代码块,它将发送请求发送到PHP文件,以便将一些数据存储在MySQL数据库中,并从PHP接收简单的json_encode()字符串,例如“error_101”响应,并且它有效正好 . 昨天我重新安装了我的XAMPP,因为我在openssl PHP扩展方面遇到了一些问题,现在我的json_encode()响应都没有返回值 . 我检查了phpinfo(),并说它启用了json支持 . 要提到从JAVA发送到PHP的值也是JSON对象,json_decode()工作得很好!

这是我将PHP响应发送到JAVA的代码:

<?php 
    header('Content-type: application/json');
    echo json_encode("error_101");
?>

这是在JAVA中获取响应的代码

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient client = new DefaultHttpClient(httpParams);

String url = "http://192.168.254.19/android/register.php";

HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();

String result = null;
if (entity != null) {
    InputStream instream = entity.getContent();

    InputStreamReader is_reader = new InputStreamReader(instream);
    BufferedReader br = new BufferedReader(is_reader);
    result = br.readLine();
    Log.i("Read from server", result);
    Toast.makeText(this,  result, Toast.LENGTH_LONG).show();
}

我得到的回应是“

3 回答

  • 1

    你确定在读取链的某个地方没有一些调试代码

    echo $TesttVar1 . '
    ';

    这也将阻止"header()"的运作 . 打开所有错误(error_reporting(E_ALL); ini_set('display_errors','on');),这将显示行
    是输出,如果是这样的话 .

    但是如果它是json_encode,为了帮助清除它,只需返回“Error_101”而没有要测试的功能 . 但我不认为你在这个项目中走得那么远 .

  • 0

    json_encode需要一个数组 . 喜欢

    json_encode(array('status'=>'error_101'));
    
  • 0

    在这种情况下:

    header("Content-type: text/html");
    echo json_encode("error_101");
    

    有用 .

    在另一个案例中:

    header("Content-type: application/json");
    echo json_encode("error_101");
    

    它不起作用 .

    这似乎是一个错误!

相关问题