0

I'm having some problems finding and using the JSON object that my app Posts to my server.
Here's my code:

InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = exmaplePrefs.getString("jsonString", "cant find json");
       // String json = "{\"twitter\":\"test\",\"country\":\"test\",\"name\":\"test\"}";

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        String status = httpResponse.getStatusLine().toString();
        // 10. convert inputstream to string
        if (!status.equals("HTTP/1.1 500 Internal Server Error")){
            if(inputStream != null){
                result = convertInputStreamToString(inputStream);   
            }
            else{
                result = "Did not work!";
            }
        }else{
            System.out.println("500 Error");

        }



    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
        System.out.println(e);
    }

    // 11. return result
    return result;
}

..

    endGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    // call AsynTask to perform network operation on separate thread
                    //new HttpAsyncTask().execute("http://posttestserver.com/post.php");    
                    new HttpAsyncTask().execute("http://rugbyone.net84.net/recieve_data.php");
        }
    });

..

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

Below is the PHP. As you can see, I've tried various different ways but cannot find the JSON. Any help would be appreciated.

<?php
    /*  Database connection  */

     if( isset($_POST["json"]) ) {  

     $data->msg = strrev($data->msg);
     $data = json_decode($_POST["json"]);
     echo json_encode($data);
     echo $GLOBALS['HTTP_RAW_POST_DATA'];
}
     $json = file_get_contents("php://input");
     $json2 = $_SERVER['HTTP_JSON'];
    // Lets parse through the JSON Array and get our individual values
    $parsedJSON = json_decode($json, true);
    var_dump($json);
    var_dump($parsedJSON);
    var_dump($json2);
    var_dump($data);


 ?> 
faintsignal
  • 1,828
  • 3
  • 22
  • 30
Chris Mowbray
  • 295
  • 1
  • 3
  • 15
  • What's your problem? Please explain and if you have error in log cat, please attach it in your question – Luc Jan 27 '14 at 01:25
  • what is the point of `$data->msg = strrev ...`? You replace `$data` on the next line anyways. And if you don't get anything form php://input, your client-side code isn't sending anything to PHP in the first place. – Marc B Jan 27 '14 at 01:26
  • they are all ways that i found to 'grab' the json object but none work – Chris Mowbray Jan 27 '14 at 01:27
  • @MarcB I tested the clientside but posting to this server - http://posttestserver.com/data/2014/01/26/15.33.18171433763 and as you can see the json object is there... – Chris Mowbray Jan 27 '14 at 01:28
  • @JackDuong no errors in logcat. I get OK 200! so the app is working correctly – Chris Mowbray Jan 27 '14 at 01:30
  • @ChrisMowbray: So, I don't understand what is your issue? Because I tested your link, It work fine. – Luc Jan 27 '14 at 01:34
  • @JackDuong yes but thats not my server....this is http://rugbyone.net84.net/recieve_data.php and i cannot see the json anywhere – Chris Mowbray Jan 27 '14 at 01:36
  • Result: string(53) "{"twitter":"Jack","country":"VN","name":"Jack Duong"}" array(3) { ["twitter"]=> string(4) "Jack" ["country"]=> string(2) "VN" ["name"]=> string(10) "Jack Duong" } NULL NULL – Luc Jan 27 '14 at 01:38
  • @JackDuong can you elaborate on this – Chris Mowbray Jan 27 '14 at 01:39
  • @ChrisMowbray I think what he's saying is that you have some extra code being putout and therefore it breaks the JSON interpreter and is Malformed. To address this, call `die()` after you return your `json_encoded()` string. – Ohgodwhy Jan 27 '14 at 01:50
  • @Ohgodwhy i have removed all unwanted code and now have this>>> still does not find the json object :( – Chris Mowbray Jan 27 '14 at 01:54
  • The JSON will still be invalid, due to the fact that you're dumping 2 objects simultaneously and they aren't separated by a `,`. – Ohgodwhy Jan 27 '14 at 02:13

1 Answers1

0

It looks like you are just sending a raw post body to your PHP site. Your PHP is expecting a post variable called json, but that is not defined in the Android code you provided.

You should use a NameValuePair like in this post:

Apache HTTP Client, POST request. How to correctly set request parameters?

Community
  • 1
  • 1
Nic Raboy
  • 3,143
  • 24
  • 26