Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a mobile client using android to consume a local REST API I have created rest server on eclipse and it is running without problems but i am trying to create my client on android studio and it is running also but when i click on the button nothing is happening.

What I have tried:

public class MainActivity extends AppCompatActivity {
    private Button btnChercher;
    private TextView tvJson;

    protected void onCreate(Bundle savedInstanceState) {

        btnChercher = (Button) findViewById(;
        tvJson = (TextView) findViewById(;
        btnChercher.setOnClickListener(new View.OnClickListener() {
                                           public void onClick(View v) {
                                               new JsonTask().execute("http://localhost:8080/membres");


    public class JsonTask extends AsyncTask<string,string,string>{

        protected String doInBackground(String... params) {
            HttpURLConnection connection=null;
            BufferedReader reader=null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();


                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line ="";

                while ((line=reader.readLine())!=null){

                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject();
                JSONArray parentArray = parentObject.getJSONArray("");
                JSONObject finalObject = parentArray.getJSONObject(0);

                String nomMembre = finalObject.getString("nomMembre");

                return nomMembre+"\n";

            } catch (MalformedURLException e) {
            } catch (IOException e) {
            } catch (JSONException e) {
            } finally {

                    try {
                    } catch (IOException e) {

            return null;

        protected void onPostExecute(String result) {

Updated 23-May-16 7:03am
Richard MacCutchan 4-May-16 12:14pm    
when i click on the button nothing is happening.
That does not really tell us anything. You need to use your debugger to find out exactly what is going on.
Member 12502641 4-May-16 13:11pm    
thinks bro but it doesn't return any exception, when i replace the url with a url of an REST api on the internet it works perfectly!!!!
a friend suggested me to replace "localhost" in the url by my ip adress when i did that and i run the client it still doesn't display anything , but i can see the Hibernaate requests in eclipse console
Richard MacCutchan 4-May-16 13:16pm    
a friend suggested
Does your friend understand the code?

The real issue is to find out what is being received by the service, and what is being sent back. And you are the only person who can find that out by further debugging.
Member 12502641 4-May-16 13:43pm    
I have created a server on eclipse using SpringBoot with a @RestController it returns a JSON object as follows:

and the code of the client is already uploaded as you can see

1 solution

localhost is current host (android app), so you should replace local host with server computer name or server IP.

http://localhost:8080/membres replace with http://server_ip:8080/membres

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900