[Android PHP] Probleme de connexion a la base de données
bonjour, je débute sur android et je suis arrivé a un point que je n'arrive pas a comprendre.
je m'explique j'ai un code ou sur une machine il marche par contre sur un autre machine( principal ) non !
le code est de ce connecté a l'aide de PHP et JSON a la base de donnée et de récuperer des données et de les afficher sur le mobile
je donne les versions
WampServer 2.4
PHP : 5.4.12
SQL : 5.6.12
APACHE 2.4.4
le code est le suivant :
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
| package com.example.envoie;
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 btnrecup;
TextView txtmsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmsg =(TextView)findViewById(R.id.txtrecup);
btnrecup =(Button)findViewById(R.id.Buton);
}
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://mon-ip-a-l'aide-de-ipconfig(fait)/andro/ess.php");
get.setURI(uri);
HttpResponse reponse= client.execute(get);
InputStream is= reponse.getEntity().getContent();
br= new BufferedReader(new InputStreamReader(is,"UTF-8"), 8192);
String ligneLue =br.readLine();
while(ligneLue != null){
sb.append(ligneLue);
sb.append("\n");
ligneLue =br.readLine();
}
}catch(Exception e){
Toast.makeText(this,"Une erreur est survenue!", Toast.LENGTH_SHORT).show();
}finally{
if(br!=null){
try{
br.close();
}catch(IOException e){
Toast.makeText(this,"Une erreur graaave est survenue!", Toast.LENGTH_SHORT).show();
}
}
}
try{
JSONArray jArray= new JSONArray(sb.toString());
txtmsg.setText(jArray.getJSONObject(0).getString("msg").toString());
}catch(JSONException je){
Toast.makeText(this,"ERROR", Toast.LENGTH_SHORT).show();
}
}
} |
le fichier ess.php comporte :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php
$base = mysql_connect ('localhost', 'root', '');
$db= mysql_select_db ('new', $base) ;
//$des=$_POST['message'];
$req = mysql_query("SELECT msg FROM message");
// On fait une boucle pour lister tout ce que contient la table :
while ($donnees = mysql_fetch_array($req) )
$sortie[]=$donnees;
$jsob= json_encode($sortie);
print ($jsob);
//mysql_close();
?> |
mon activity_main.xml est :
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="fill_parent"
android:layout_height="fill_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/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Buton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtrecup"
android:layout_alignParentTop="true"
android:layout_marginTop="49dp"
android:text="@string/button1"
android:onClick="recupererMessage" />
<TextView
android:id="@+id/txtrecup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Buton"
android:layout_marginTop="56dp"
android:text="@string/textView1" />
</RelativeLayout> |
Mon Manifest est :
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
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.envoie"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="2"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.envoie.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> |
je ne comprends pas pourquoi je n'arrive pas a récup:
PS : quand je click sur Recuperer c'est le TOAST : ERROR qui m'affiche.