D
D
Dmitry Richter2015-03-05 18:57:49
PHP
Dmitry Richter, 2015-03-05 18:57:49

Problem with saving Json data in PhP?

Sending data as json string to PHP server. The server sees them and creates a file for them, but does not save them, the server also gives an answer. Since I am a complete zero in PHP, I would like to get help, your help. So why doesn't the server save the data?
Here is the processing code:

<?php
  $r = $_POST['req'];

  $fn = fopen('reqest.txt','a+');
  fwrite($fn, $r);
  fclose($fn);

$arr = array('status'=>ok,'code'=>2,'monitor_frequency'=>2);
// $test = array();
echo json_encode($arr);

?>

Here is the send code from the Android app:
static  String SendJsonViaPost(String url, String json) {

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            try {
               // StringEntity stringEntity = new StringEntity(json, "UTF-8");
                //stringEntity.setContentType("application/json");
                //post.setEntity(stringEntity);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("uid",m_szUniqueID));
                nameValuePairs.add(new BasicNameValuePair("data",json));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
                HttpResponse response = client.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    responseJson += line;
                }

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

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Surzhikov, 2015-03-05
@Surzhikov

Do it like this

<?php
  $r = $_POST['req'];
  var_dump($r);

and tell me what it is..

M
Maxim Gavrilov, 2015-03-05
@thestump

file_put_contents('file.json', $json);

D
Dmitry, 2015-03-07
@Thelema

You pass 2 variables from Android to the server to the post "uid" and "data". And on the server you are trying to catch the "req" variable. inconsistency

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question