Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883

    Come ricevere dei dati json in una pagina php

    Devo creare una pagina php, index_test.php, che riceva dei dati in formato json da una posta (la post è fatta in java), non riesco a capire come fare a ricevere i dati in php, questo è quello che penso vada scritto

    Codice PHP:
    <?php        
    $json 
    $_POST[??????];    
    $data json_decode($jsonTRUE); ?>
    cosa devo scrivere al posto dei punti interrogativi?



    questa è la chiamata, in java, che mandata i dati (email e password)
    [JAVA]
    private String SendDataFromAndroidDevice() {
    String result = "";



    try {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("mail", mEmail);
    jsonObject.put("password", mPassword);

    String url = "http://www.miosito.it/index_test.php";

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

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

    // 4. convert JSONObject to JSON to String
    String json = jsonObject.toString();

    // 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 inputStream = httpResponse.getEntity().getContent();

    // 10. convert inputstream to string
    if(inputStream != null)
    result = convertInputStreamToString(inputStream);
    else
    result = "Did not work!";




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

    [/JAVA]

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,501
    Con Java non ho mai mandato dati via post in questo modo, ma suppongo che da qualche parte si debba dare un nome ai dati inviati.

    Intanto puoi provare a fare una cosa: sullo script PHP mettti una cosa di questo tipo
    Codice PHP:
    $dati print_r($_POST);

    $file fopen('datipost.txt','r+');

    fwrite($file$dati);
    fclose($file); 
    così nella stessa cartella che contiene lo script php ti troverai un file di testo contenente ciò che è arrivato tramite post, da lì puoi leggere il nome da usare per prendere i dati.

    Nota di servizio: i tag utilizzabili per il codice sono solo [php] per codice php e [code] per tutto il resto

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.