Bonjour, lorsque j'exécute mon application android qui recupére des données du base de données MySQL vers Android l'émulateur m'affiche cette erreur :
"the application has stopped unexpectedly android" s'il vous plait aidez moi
et voila mon code java:
package com.example.gsctutoo;
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.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
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.lbl_boutton);
txtMsg= (Button)findViewById(R.id.lbl_msg);
}
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.6/eclipse/gsctuto/envoi.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, "Erreur", Toast.LENGTH_SHORT).show();
}
finally
{
if (br != null) {
try {
br.close();
}
catch (IOException e){
Toast.makeText(this, "Erreur grave", 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, "Erreur est servenue", Toast.LENGTH_SHORT).show();
}
}
}
}
et voila logCat:
02-26 21:39:43.955: D/AndroidRuntime(314): Shutting down VM
02-26 21:39:43.955: W/dalvikvm(314): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-26 21:39:43.965: E/AndroidRuntime(314): FATAL EXCEPTION: main
02-26 21:39:43.965: E/AndroidRuntime(314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gsctutoo/com.example.gsctutoo.MainActivity}: java.lang.ClassCastException: android.widget.TextView
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 21:39:43.965: E/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 21:39:43.965: E/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521)
02-26 21:39:43.965: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 21:39:43.965: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 21:39:43.965: E/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method)
02-26 21:39:43.965: E/AndroidRuntime(314): Caused by: java.lang.ClassCastException: android.widget.TextView
02-26 21:39:43.965: E/AndroidRuntime(314): at com.example.gsctutoo.MainActivity.onCreate(MainActivity.java:33)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 21:39:43.965: E/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-26 21:39:43.965: E/AndroidRuntime(314): ... 11 more
et voila le code php dans eclipse php:
<?php
$Host="localhost:3306";
$User="root";
$Password="root";
$bd=mysql_connect($Host,$User,$Password);
mysql_select_db('gsctuto,$bd');
$sql=mysql_query("setect message from information");
while ($result= mysql_fetch_assoc($sql))
{
$sortie[] = $result;
$jsbd= json_encode($sortie);
print ($jsbd);
}
Afficher la suite