简体   繁体   English

无法从Android中的PHP文件接收JSON字符串

[英]Can't receive JSON string from PHP file in Android

below I include some of my code: 下面,我包括一些代码:

Here is my code: 这是我的代码:

int responseCode = httpCon.getResponseCode();

if (responseCode==-1) { httpCon.connect(); }

InputStream is = httpCon.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
   response.append(line);
}
rd.close();

try {

    OutputStream file = openFileOutput("configND", Context.MODE_PRIVATE);
    DataOutputStream wrf = new DataOutputStream(file);
    wrf.writeBytes(line);
    wrf.close();

    } catch (Exception e){
    e.printStackTrace();
}

I'm trying receive data (JSON string) from PHP file and then create a file with a content but something is wrong. 我正在尝试从PHP文件接收数据(JSON字符串),然后创建一个包含内容的文件,但是出了点问题。 I can't receive a JSON string and the file can not be created. 我无法接收JSON字符串,并且无法创建文件。

I use HttpURLconnection and I don't use apache library. 我使用HttpURLconnection,而不使用apache库。 I might add that sending JSON is working properly. 我可能会添加发送JSON的功能。

Help! 救命!

If you receive nothing from the server, the problem might be simply in your PHP script. 如果您没有从服务器获得任何信息,则问题可能出在您的PHP脚本中。

For the Android part of the "problem", if you don't want to use Volley and simply handle this with Android APIs, these two links should make your day :) 对于“问题”中的Android部分,如果您不想使用Volley并仅使用Android API来处理此问题,那么这两个链接应该可以帮助您:

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php

Personnally, when I learned Android some years ago, I followed AndroidHive ressources and it works pretty well. 就个人而言,几年前我学习Android时,我遵循了AndroidHive资源,并且运行良好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM