Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Connection à une base de données externes : Erreur HttpHostConnection refused

Featured Replies

Posté(e)

Bonjour, 

Je débute en programmation Android et je cherche à me connecter à une base de données externes sous Android; pour cela, j'utilise la librairie HttpClient mais quand je lance mon application, cette erreur survient dans le logcat :

Error in http connection org.apache.http.conn.HttpHostConnectException:Connection to http://10.0.2.2:8080 refused

 

Voici mon code :

public class ArticleActivity extends AppCompatActivity {

    ListView listeArticles;
    String[] articles = new String[]{"article1", "article2", "article3", "article4"};
    HttpPost httpPost;
    HttpClient client;
    HttpResponse response;
    HttpEntity entity;
    InputStream is;
    String result = "";

    @[member=override]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            System.out.println("*** My thread is now configured to allow connection");
        }

        try
        {
            client = new DefaultHttpClient();
            httpPost = new HttpPost("http://10.0.2.2:8080/GestionArticle/connection.php");
            response = client.execute(httpPost);
            entity = response.getEntity();
            is = entity.getContent();
        }
        catch(Exception e)
        {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while(reader.readLine()!= null)
            {
                line = reader.readLine();
                sb.append(line + "\n");
            }
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","id: "+json_data.getInt("idArticle")+
                                ", titre: "+json_data.getString("titre")+
                                ", image: "+json_data.getString("image")+
                                ", description: "+json_data.getString("description")+
                                ", contenu: "+json_data.getString("contenu")+
                                ", dateArticle: "+json_data.getString("dateArticle")+
                                ", heureArticle: "+json_data.getString("heureArticle")
                );
            }
        }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
         }


        listeArticles=(ListView)findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ArticleActivity.this,
                android.R.layout.simple_list_item_1, articles);
        listeArticles.setAdapter(adapter);


    }
}

et mon manifest :

<uses-permission android:name="android.permission.INTERNET"/>
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".AccueilActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ContactActivity" />
        <activity android:name=".AvisActivity" />
        <activity android:name=".LocalisationActivity" />
        <activity android:name=".ArticleActivity"></activity>
    </application>

Quelqu'un saurait il comment corriger cette erreur ? 

Merci d'avance ! :)

 

 
Posez votre question
Signaler Android : Erreur http connection refused
 
Bonjour, 

Je cherche à me connecter à une base de données externes sous Android; pour cela, j'utilise la librairie HttpClient mais quand je lance mon application, cette erreur survient dans le logcat : 
http://10.0.2.2:8080 refused

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.