Jump to content
Sign in to follow this  
aminosmas

récupération des données JSON pour les insérer dans MYSQL !

Recommended Posts

voila mon code android : 

 

 

class envoi_doc_entete extends AsyncTask<String, String, String> {

 
 
protected String doInBackground(String... args) {
 
 
doc_dao = new Document_entete_DAO(context);
JSONObject object = new JSONObject();
List<NameValuePair> params = new ArrayList<NameValuePair>();
 
for(Document_entete doc : doc_dao.selectionner_Document_entete() ){
 
params.add(new BasicNameValuePair("ID_DocVente",doc.getiD_DocVente()));
params.add(new BasicNameValuePair("Souche",  doc.getSouche()));
params.add(new BasicNameValuePair("ID_client", doc.getiD_client()));
params.add(new BasicNameValuePair("Date", doc.getDate()));
params.add(new BasicNameValuePair("Type_DocVente", Integer.toString(doc.getType_DocVente())));
params.add(new BasicNameValuePair("ID_Agent_commercial", doc.getiD_agent_commercial()));
params.add(new BasicNameValuePair("Date_livraison",doc.getDate_livraison() ));
params.add(new BasicNameValuePair("iD_depot",  doc.getID_Depot()));
 
 
       }
 
JSONObject json = jParser.makeHttpRequest(url_envoi_doc_entete,
"POST", params);
 
// verifier les données dans params 
Log.d("Create Response", params.toString());
 
try {
int success = json.getInt(TAG_SUCCESS);
 
if (success == 1) {
 
Log.d("Create Response", "enregistrement OK");
 
 
} else {
Log.d("Create Response", "enregistrement echoué ! ");
}
} catch (JSONException e) {
e.printStackTrace();
}
 
return null;
}
 
 
 
}  
 
 
 
 
code "JSONParser ":
 
public class JSONParser {
 
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
 
// constructor
public JSONParser() {
 
}
 
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
 
// Making HTTP request
try {
 
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
 
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
 
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
 
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
 
 
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
 
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
 
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
 
// return JSON String
return jObj;
 
}
}
 
 
Mon code PHP :
 
 
<?php
 
$response = array();
 
 
if (isset($_POST['ID_DocVente']) && isset($_POST['Souche']) && isset($_POST['ID_client'])&& isset($_POST['Date'])&& isset($_POST['Type_DocVente'])&& isset($_POST['ID_Agent_commercial'])&& isset($_POST['Date_livraison'])&& isset($_POST['iD_depot'])) {
 
    $ID_DocVente = $_POST['ID_DocVente'];
    $Souche = $_POST['Souche'];
    $ID_client = $_POST['ID_client'];
$Date = $_POST['Date'];
$Type_DocVente = $_POST['Type_DocVente'];
$ID_Agent_commercial = $_POST['ID_Agent_commercial'];
$Date_livraison = $_POST['Date_livraison'];
$iD_depot = $_POST['iD_depot'];
mysql_connect("localhost","root","mysql");
mysql_select_db("mediasoftadistance");
 
    // inserer une ligne
    $result = mysql_query("INSERT INTO Doc_entete(ID_DocVente, Souche, Date,ID_client,Type_DocVente,ID_Agent_commercial,Date_livraison,iD_depot) VALUES('$ID_DocVente', '$Souche', '$Date','$ID_client','$Type_DocVente','$ID_Agent_commercial','$Date_livraison','$iD_depot')");
   
 
    // verifier l'insertion
    if ($result) {
        // succée
        $response["success"] = 1;
        $response["message"] = "document successfully created.";
 
        echo json_encode($response);
    } else {
        // echoué
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
        
        echo json_encode($response);
    }
} else {
    
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    echo json_encode($response);
}
?>
 
 
 
lorsque j’exécute mon application le script PHP insère la dernière ligne (la dernière document) . aidez moi svp !  

aucune réponse :/

Share this post


Link to post
Share on other sites

Bonjour,

 

Merci

 - d'être un peu plus précis dans ta question/explication de ce que tu essaies de faire (erreurs rencontrés ? traces de logs ? ...) plutôt que de presque se contenter d'un simple "aidez-moi" ...

 - d'utiliser les balises [ code ] dans ton post pour délimiter tes extraits de code, pour une meilleure lisibilité du post

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  





×
×
  • Create New...