Bonjour,

Dans le cadre d'un stage, je dois réaliser une application mobile Android sous Android Studio permettant d'afficher des articles hébergés sur une base de données externes à l'application.

Etant débutant en programmation Android, j'ai suivi un tuto qui m'a conduit à utiliser la librairie HttpClient pour l'accès à ma base de donnée.

Voici, la partie du code que j'ai faite :

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
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
 
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 = "";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);
 
        try
        {
            client = new DefaultHttpClient();
            httpPost = new HttpPost("http://192.168.x.x/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 ma page php :

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
<?php
 
	$connection = mysqli_connect('localhost', 'root', '', 'articles');
	if(!$connection)
	{
		die("Connection échoué :".mysqli_error);
	}
	$req=mysqli_query($connection, "SELECT * FROM articles ORDER BY idArticle DESC");
	while($row=mysqli_fetch_assoc($req))
        $output[]=$row;
 
	print(json_encode($output));
 
	mysqli_close();
 
?>
J'ai commencé par importer la libraire HttpClient 4.2.4 mais elle ne reconnaissait pas HttpResponse, alors j'ai importé toutes celles que j'ai trouvé sur ce zip :
http://search.maven.org/#search%7Cga...nt-assembly%22

HttpResponse est bien reconnu cette fois, mais l'application ne démarre plus et j'ai cette erreur :
you in this endeavor. If you find that you cannot do this, then
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
trouble processing "javax/transaction/HeuristicCommitException.class":
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.
However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.
If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.
If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.
If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.
1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 1
Apparemment il y a un problème avec mes librairies, voici mon build gradle :
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
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
 
    defaultConfig {
        applicationId "com.example.petotmarc.cftcfranche_comte"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    //compile files('libs/okhttp-3.2.0.jar')
    // compile files('libs/httpclient-4.2.4.jar')
    //compile files('libs/apache-httpcomponents-httpclient.jar')
    //compile files('libs/httpclient-4.0.1.jar')
    //compile files('libs/httpcore-4.0.1.jar')
    compile files('libs/google-http-client-android-1.22.0.jar')
    //compile files('libs/google-http-client-1.22.0.jar')
    //compile files('libs/commons-logging-1.1.1.jar')
}
Donc voilà, toute aide n'est pas de refus car j'ai plus beaucoup de temps.. Si quelqu'un connait la solution, ce serait cool

Merci d'avance !