Erreur de récupération des données
l'application affiche le message d'exception et ne se marche pas aidez moi svp
le MainActivity:
Code:
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
| package com.gsc.gsctutoandro;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
Button btnRecuperer;
TextView txtMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRecuperer = (Button)findViewById(R.id.btnRecuperer);
txtMsg = (TextView)findViewById(R.id.lblMsg);
}
public void recupererMessage(View v)
{
StringBuffer sb = new StringBuffer("");
BufferedReader br = null;
try{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet();
URI uri = new URI("http://192.168.1.1/eclipse/gsctutoandro/envoie.php");
get.setURI(uri);
HttpResponse reponse = client.execute(get);
InputStream is = reponse.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String ligneLue = br.readLine();
while(ligneLue != null)
{
sb.append(ligneLue);
sb.append("\n");
ligneLue = br.readLine();
}
}catch(Exception e){
Toast.makeText(this, "un erreur est servenue !", Toast.LENGTH_SHORT).show();
}
finally{
if (br != null){
try{
br.close();
}catch(IOException e){
Toast.makeText(this, "un erreur grave est servenue !", Toast.LENGTH_SHORT).show();
}
}
}
try{
JSONArray jArray = new JSONArray(sb.toString());
txtMsg.setText(jArray.getJSONObject(0).getString("message").toString());
}catch(JSONException je){
Toast.makeText(this, "un erreur grave est servenue !", Toast.LENGTH_SHORT).show();
}
}
} |
et ça le code php
Code:
1 2 3 4 5 6 7 8 9 10
| <?php
$db = mysql_connect ('localhost', 'root','');
mysql_select_db('gsctuto', $db);
$sql = mysql_query("SELECT message FROM information");
while($resultat = mysql_fetch_assoc($sql))
$sortie[] = $resultat;
$jsob = json_encode($sortie);
print($jsob);
?> |
et ca l'activitymain.xml
Code:
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
| <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=".MainActivity" >
<TextView
android:text="@string/lbl_bienvenue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/btnRecuperer"
android:text="@string/lbl_bouton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="recupererMessage"
/>
<TextView
android:id="@+id/lblMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_marginTop="90dp"
/>
</RelativeLayout> |
et ca AndroidManifest
Code:
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
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gsc.gsctutoandro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gsc.gsctutoandro.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |