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

Android Discussion :

Contacter base MySQl


Sujet :

Android

  1. #1
    Futur Membre du Club
    Inscrit en
    Février 2009
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 16
    Points : 8
    Points
    8
    Par défaut Contacter base MySQl
    Bonjour,
    Voila plusieurs jours que j'essaie de faire communiquer mon application mobile avec une base mysql.
    J'ai un script php qui me retourne un tableau JSON du style {"id":"4793","gender":"woman","age":"25-34","occupation":"Doctor - Health care"}

    avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     echo json_encode($user);
    J'utilise asynctack et http client pour recuperer le json.
    Le 'doinbackground' semble bien se derouler, c'est lorsque j'essaie de mettre a jour ma textview avec une valeur que l'application plante. Je ne trouve pas pourquoi : /

    Voici mon activity:
    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
     
    package com.example.recmovieapp;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;
     
     
     
     
     
     
     
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;
     
    public class UserInfoActivity extends Activity 
    {
    	TextView tID;
    	TextView tGender;
    	TextView tAge;
    	TextView tOccupation;
     
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
     
    		tID = (TextView)findViewById(R.id.UserId);
    		tGender = (TextView)findViewById(R.id.UserGender);
    		tAge = (TextView)findViewById(R.id.UserAge);
    		tOccupation = (TextView)findViewById(R.id.UserOccupation);
     
    		GetUserInfo myThread = new GetUserInfo(UserInfoActivity.this);
    		myThread.execute();
     
    	}
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) 
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
     
    	public class GetUserInfo extends AsyncTask<String, Void, String>
    	{
     
    		Context mContext = null;
    		Exception exception = null;
    		User myUser;
    		int idUser;
     
    		GetUserInfo(Context context)
    		{
    			mContext = context;
    		}
     
    		@Override
    		protected String doInBackground(String... arg0) 
    		{
     
    			try
    			{
    				//Create the HTTP request
    				HttpParams httpParameters = new BasicHttpParams();
     
    				//Setup timeouts
    				HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
    				HttpConnectionParams.setSoTimeout(httpParameters, 15000);			
     
    				HttpClient httpclient = new DefaultHttpClient(httpParameters);
    				HttpPost httppost = new HttpPost("http://kevin.pinero.alwaysdata.net/RecMovie/db.php");      
    				HttpResponse response = httpclient.execute(httppost);
    				HttpEntity entity = response.getEntity();
     
    				String result = EntityUtils.toString(entity);
     
    				// Create a JSON object from the request response
    				JSONObject jsonObject = new JSONObject(result);
     
    				//Retrieve the data from the JSON object
    				idUser = jsonObject.getInt("id");
     
    			}
    			catch (Exception e)
    			{
    				Log.e("doInBackground", "Error:", e);
    				exception = e;
    			}
     
    			return null;
    		}
     
    		@Override
    		protected void onPostExecute(String str)
    		{
    			//Update the UI
    			tID.setText("ID : "+idUser);
    		}
     
    	}
    }
    Et voici mon activity description :
    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
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".UserInfoActivity" >
     
        <TextView
            android:id="@+id/UserId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ID :"
            android:textAppearance="?android:attr/textAppearanceMedium"
         />
     
        <TextView
            android:id="@+id/UserGender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/UserId"
            android:layout_below="@+id/UserId"
            android:text="Gender :"
            android:textAppearance="?android:attr/textAppearanceMedium"
         />
     
        <TextView
            android:id="@+id/UserAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/UserGender"
            android:layout_below="@+id/UserGender"
            android:text="Age :"
            android:textAppearance="?android:attr/textAppearanceMedium"
         />
     
        <TextView
            android:id="@+id/UserOccupation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/UserAge"
            android:layout_below="@+id/UserAge"
            android:text="Occupation:"
            android:textAppearance="?android:attr/textAppearanceMedium"
         />
    </RelativeLayout>
    Voici mon logcat :
    08-19 13:41:35.688: E/AndroidRuntime(1985): FATAL EXCEPTION: main
    08-19 13:41:35.688: E/AndroidRuntime(1985): java.lang.NullPointerException
    08-19 13:41:35.688: E/AndroidRuntime(1985): at com.example.recmovieapp.UserInfoActivity$GetUserInfo.onPostExecute(UserInfoActivity.java:113)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at com.example.recmovieapp.UserInfoActivity$GetUserInfo.onPostExecute(UserInfoActivity.java:1)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.os.AsyncTask.finish(AsyncTask.java:631)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.os.AsyncTask.access$600(AsyncTask.java:177)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.os.Handler.dispatchMessage(Handler.java:99)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.os.Looper.loop(Looper.java:137)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at android.app.ActivityThread.main(ActivityThread.java:5041)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at java.lang.reflect.Method.invokeNative(Native Method)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at java.lang.reflect.Method.invoke(Method.java:511)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    08-19 13:41:35.688: E/AndroidRuntime(1985): at dalvik.system.NativeStart.main(Native Method)

    Si quelqu'un avait une idée ou pourrait me corriger ce serait vraiment super !
    Merci

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    757
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 757
    Points : 968
    Points
    968
    Par défaut
    L'erreur provient de cette ligne, suite à un NullPointerException
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tID.setText("ID : "+idUser);
    Cela veut dire que la variable tID est non définie.

  3. #3
    Futur Membre du Club
    Inscrit en
    Février 2009
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 16
    Points : 8
    Points
    8
    Par défaut
    Ok c'est ce que je pensais mais tID me semble bien initialisé non ?

    Ma textview dans le XML se nomme UserID et ensuite je fais un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tID = (TextView)findViewById(R.id.UserId);
    pourquoi il reconnait pas tID ?

  4. #4
    Membre éprouvé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    757
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 757
    Points : 968
    Points
    968
    Par défaut
    Le layout utilisé est-il le bon ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    setContentView(R.layout.activity_main);
    La classe R importée est-elle celle de ton projet ?
    Eclipse ne souligne rien en rouge ?

  5. #5
    Futur Membre du Club
    Inscrit en
    Février 2009
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 16
    Points : 8
    Points
    8
    Par défaut
    Wow ! je te remercie ! l'erreur venait du layout je choisissais le mauvais ... quand je pense ça fait des jours que je cherche cette erreur ...

    Merci beaucoup de ton aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Exporter les contacts Outlook vers une base MySQL ?
    Par Mister Paul dans le forum Outlook
    Réponses: 7
    Dernier message: 31/03/2008, 11h04
  2. [FLASH+PHP+MySQL] Comment contacter une base MySQL
    Par pierrot10 dans le forum Dynamique
    Réponses: 3
    Dernier message: 23/08/2007, 19h20
  3. Réponses: 3
    Dernier message: 12/11/2003, 00h34
  4. recréer une base mysql
    Par ryan dans le forum Administration
    Réponses: 2
    Dernier message: 27/06/2003, 23h04
  5. [Dev-C++] Accès à une base MySQL
    Par Beetlejuice dans le forum Dev-C++
    Réponses: 8
    Dernier message: 08/05/2003, 13h17

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