简体   繁体   中英

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. I can't receive a JSON string and the file can not be created.

I use HttpURLconnection and I don't use apache library. I might add that sending JSON is working properly.

Help!

If you receive nothing from the server, the problem might be simply in your PHP script.

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 :)

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

Personnally, when I learned Android some years ago, I followed AndroidHive ressources and it works pretty well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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