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"}
avecJ'utilise asynctack et http client pour recuperer le json.
Code : Sélectionner tout - Visualiser dans une fenêtre à part echo json_encode($user);
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:
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
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); } } }
Voici mon logcat :
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>
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
Partager