IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

comment envoyer des données choisies dans un spinner à la base de données mysql dans le serveur "web service"


Sujet :

API standards et tierces Android

  1. #1
    Nouveau membre du Club
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2015
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2015
    Messages : 56
    Points : 29
    Points
    29
    Par défaut comment envoyer des données choisies dans un spinner à la base de données mysql dans le serveur "web service"
    Bonjour,
    j’essaye de réaliser un code dans je peux choisir une données dans le spinner et l’envoyer a la base dans le serveur voici mon code pour mieux comprendre ,
    se code m'affiche les données mais de l'envoyer a la base s marche pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     
    /**
     * Created  on 31/05/2016.
     */
     
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
     
    public class petitessai extends Activity {
     
        private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();
     
        Spinner inputnom;
     
        Button btnajou;
        InputStream is = null;
        String result = null;
        String line = null;
        String[] client, codclient, prod, codprd;
     
        Spinner spinner1, spinner2, spinner3, spinner4;
        EditText code;
     
        private static String url_create_product = "http://192.168.1.16/AJOUTER.php";
        private static final String TAG_SUCCESS = "success";
        //*************
     
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.essai);
     
            btnajou = (Button) findViewById(R.id.clic);
     
            btnajou.setOnClickListener(new View.OnClickListener() {
     
                @Override
                public void onClick(View view) {
     
                    new CreateNew().execute();
                }
            });
     
     
     
     
            final List<String> list1 = new ArrayList<String>();
     
     
            // TODO Auto-generated method stub
     
     
            spinner2 = (Spinner) findViewById(R.id.SP);
     
     
     
     
            final List<String> list3 = new ArrayList<String>();
            final List<String> list4 = new ArrayList<String>();
            //afficher les données dans le spinner
     
            // TODO Auto-generated method stub
     
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.16/modpai.php");
                HttpResponse response = httpclient.execute(httppost);
                Log.e("Fail 1", "3");
     
                HttpEntity entity = response.getEntity();
                Log.e("Fail 1", "4");
     
                is = entity.getContent();
                Log.e("Pass 1", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show();
                finish();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("Fail 2", e.toString());
            }
     
     
            try {
                JSONArray JA = new JSONArray(result);
                JSONObject json = null;
                client = new String[JA.length()];
     
     
                for (int i = 0; i < JA.length(); i++) {
                    json = JA.getJSONObject(i);
                    client[i] = json.getString("NAME");
                }
                Toast.makeText(getApplicationContext(), "sss", Toast.LENGTH_LONG).show();
     
                for (int i = 0; i < client.length; i++) {
                    list3.add(client[i]);
                }
     
                spinner_fn();
     
            } catch (Exception e) {
     
                Log.e("Fail 3", e.toString());
    //login.this.finish();
     
            }
        }
     
     
        private void spinner_fn() {
            // TODO Auto-generated method stub
     
            ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_spinner_item, client);
            dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(dataAdapter1);
            spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
     
            {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
    // TODO Auto-generated method stub
     
     
                }
     
     
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
                }
     
            });
        }
     
        // ajouter la données choisi dans le spinner a la table commande
     
     
        class CreateNew extends AsyncTask<String, String, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(petitessai.this);
                pDialog.setMessage("patientez svp.............................................................");
     
     
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }
     
            protected String doInBackground(String... args) {
                inputnom = (Spinner) findViewById(R.id.spinner2);
     
                String nom = inputnom.getSelectedItem().toString();
     
     
                List<NameValuePair> params = new ArrayList<NameValuePair>();
     
     
                params.add(new BasicNameValuePair("NAME", nom));
     
                JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);
     
     
     
                Log.d("Create Response", json.toString());
     
                try {
                    int success = json.getInt(TAG_SUCCESS);
     
                    if (success == 1) {
     
                        Intent i = new Intent(getApplicationContext(), Afficher_listCL.class);
                        startActivity(i);
     
     
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
     
                return null;
            }
     
     
            protected void onPostExecute(String file_url) {
     
                pDialog.dismiss();
            } } }
    dés que je clic sur le button pour envyer la données choisi , j'aurai les erreur suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    06-07 12:06:29.975      675-675/riro15.exemple_web_service E/Fail 13
    06-07 12:06:29.975      675-675/riro15.exemple_web_service E/Fail 14
    06-07 12:06:29.995      675-675/riro15.exemple_web_service E/Pass 1﹕ connection success
    06-07 12:26:27.575      713-713/riro15.exemple_web_service E/Fail 13
    06-07 12:26:27.575      713-713/riro15.exemple_web_service E/Fail 14
    06-07 12:26:27.575      713-713/riro15.exemple_web_service E/Pass 1﹕ connection success
    06-07 12:26:28.325      713-713/riro15.exemple_web_service E/Fail 13
    06-07 12:26:28.325      713-713/riro15.exemple_web_service E/Fail 14
    06-07 12:26:28.335      713-713/riro15.exemple_web_service E/Pass 1﹕ connection success
    06-07 12:29:55.874      713-713/riro15.exemple_web_service E/Fail 13
    06-07 12:29:55.874      713-713/riro15.exemple_web_service E/Fail 14
    06-07 12:29:55.894      713-713/riro15.exemple_web_service E/Pass 1﹕ connection success
    06-07 12:30:03.695      713-726/riro15.exemple_web_service E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
        java.lang.RuntimeException: An error occured while executing doInBackground()
                at android.os.AsyncTask$3.done(AsyncTask.java:200)
                at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
                at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
                at java.lang.Thread.run(Thread.java:1019)
         Caused by: java.lang.NullPointerException
                at riro15.exemple_web_service.petitessai$CreateNew.doInBackground(petitessai.java:200)
                at riro15.exemple_web_service.petitessai$CreateNew.doInBackground(petitessai.java:184)
                at android.os.AsyncTask$2.call(AsyncTask.java:185)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    ************at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    ************at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    ************at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    ************at java.lang.Thread.run(Thread.java:1019)
    06-07 12:30:07.685      713-713/riro15.exemple_web_service E/WindowManager﹕ Activity riro15.exemple_web_service.petitessai has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4062bee8 that was originally added here
        android.view.WindowLeaked: Activity riro15.exemple_web_service.petitessai has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4062bee8 that was originally added here
                at android.view.ViewRoot.<init>(ViewRoot.java:258)
                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                at android.view.Window$LocalWindowManager.addView(Window.java:424)
                at android.app.Dialog.show(Dialog.java:241)
                at riro15.exemple_web_service.petitessai$CreateNew.onPreExecute(petitessai.java:194)
                at android.os.AsyncTask.execute(AsyncTask.java:391)
                at riro15.exemple_web_service.petitessai$1.onClick(petitessai.java:75)
                at android.view.View.performClick(View.java:2485)
                at android.view.View$PerformClick.run(View.java:9080)
                at android.os.Handler.handleCallback(Handler.java:587)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:123)
                at android.app.ActivityThread.main(ActivityThread.java:3683)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:507)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
                at dalvik.system.NativeStart.main(Native Method)

    quelqu'un à une reponse svp ;merci a l'avance

  2. #2
    Membre régulier
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2012
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2012
    Messages : 100
    Points : 91
    Points
    91
    Par défaut
    Bonjour,

    Premièrement tu peux vérifier si le paramètre name que tu as placé dans ce code se trouve aussi dans votre script php dans la variable $_POST['NAME']/
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    params.add(new BasicNameValuePair("NAME", nom));
    Deuxième dans la méthode doInBackground, je constate que vous effectuer le traitement de votre du résultat en demandant de faire appel à une autre activité.
    D'après ce que je connais le resultat de la méthode doInBackground est récupéré par la méthode Post onPostExecute(String file_url) et la valeur du résultat est stockée dans le paramètre de cette méthode.

    C'est dans cette méthode que tu peux faire le test de ton résultat et faire la redirection vers une autre activité. Pour finir la méthode doInBackground que tu as crée doit retourné un String et non un null comme je le vois. Si cette valeur reste nulle alors la méthode onPostExecute n'aurait pas le vrai résultat provenant de ta base de donnée, elle aura toujours un résultat nulle.

Discussions similaires

  1. Comment envoyer des données dans le bigdata ?
    Par MichaelREMY dans le forum Big Data
    Réponses: 1
    Dernier message: 23/06/2015, 22h00
  2. Réponses: 9
    Dernier message: 14/06/2013, 10h34
  3. Réponses: 5
    Dernier message: 09/10/2008, 19h14
  4. Réponses: 3
    Dernier message: 19/09/2006, 15h36
  5. Réponses: 16
    Dernier message: 20/03/2006, 23h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo