0

how to read Json data sent by a php file in android

here is the php code

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);  
echo $data_string;

here is the code to read data android json

InputStream instream = entity.getContent();

// String result = "";
Log.i("--------etat---------r","-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-");

String result = this.convertStreamToString(instream);
Log.i("--------etat---------r","-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-");

here is the method to analyze the InputStream object

public  String convertStreamToString(InputStream is) {

        convertStreamToString " );

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;
        try {

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");

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

        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }

        return sb.toString();

    }

but here is the code that is displayed

11-15 10:46:58.744: I/Read from server(526): 11-15 10:46:58.744: I/Read from server(526): ( ! ) SCREAM: Error suppression ignored for 11-15 10:46:58.744: I/Read from server(526): ( ! ) Notice: Undefined index: json in C:\wamp\www\younes_project\younesservices.php on line 4 11-15 10:46:58.744: I/Read from server(526): Call Stack 11-15 10:46:58.744: I/Read from server(526): #TimeMemoryFunctionLocation 11-15 10:46:58.744: I/Read from server(526): 10.0006142200{main}( )..\younesservices.php:0 11-15 10:46:58.744: I/Read from server(526): 11-15 10:46:58.744: I/Read from server(526): {"name":"Hagrid","age":"36"}

but i have read only variable

this :{"name":"Hagrid","age":"36"}

  • [A search for 'Android JSON' revealed this](http://developer.android.com/reference/org/json/package-summary.html). There's also an error in your PHP file, with an undefined index that you're trying to find on `younessservices.php` at line 4. – h2ooooooo Apr 13 '13 at 10:58
  • Have you seen [Sending and Parsing JSON in Android](http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android)? – devnull Apr 13 '13 at 11:04

1 Answers1

0

use this code.

public String getResult(String url) {

    Log.v(TAG, "Final Requsting URL is : :"+url);

    String line = "";
    String responseData = null;

    try {
        StringBuilder sb = new StringBuilder();
        String x = "";
        URL httpurl = new URL(url);
        URLConnection  tc= httpurl.openConnection();


        /*URLConnection tc = twitter.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) tc;
        httpConnection.setRequestMethod("POST");*/
        //tc.setRe
        //tc.setDoOutput(false);

        BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

        while ((line = in.readLine()) != null) {
            sb.append(line + "\n");
            x = sb.toString();
        }
        responseData = new String(x);
    }

   catch (FileNotFoundException e) {
       Log.v("NewWebHelper", "FileNotFoundException :");
        e.printStackTrace();
     }
    catch (IOException e) {
        Log.v("NewWebHelper", "IOException :");
        e.printStackTrace();
    }
  catch (Exception e) {
      Log.v("NewWebHelper", "Exception :");
        e.printStackTrace();
    }
    return responseData;
}
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • here is the complete code of the web service on android to send a json object in php file and retrieve an object json – EL AFRIT Meriem Apr 13 '13 at 11:28
  • how to retrieve the variables sent by php and ignore others server(526): {"name":"Hagrid","age":"36"} , you find other information in the subject Question – EL AFRIT Meriem Apr 13 '13 at 11:52