Click here to Skip to main content
14,669,324 members
Rate this:
Please Sign up or sign in to 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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


                                       }
        );
    }


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

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

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

                connection.connect();

                InputStream stream = connection.getInputStream();

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

                StringBuffer buffer = new StringBuffer();

                String line ="";

                while ((line=reader.readLine())!=null){
                    buffer.append(line);
                }

                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) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if(connection!=null){
                    connection.disconnect();
                }

                if(reader!=null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            tvJson.setText(result);
        }
    }
}
Posted
Updated 23-May-16 7:03am
v2
Comments
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:
[{"idMembre":1,"nomMembre":"Hassen"},{"idMembre":2,"nomMembre":"Hassen"},{"idMembre":3,"nomMembre":"Hassen"},{"idMembre":4,"nomMembre":"Hassen"},{"idMembre":5,"nomMembre":"Hassen"},{"idMembre":6,"nomMembre":"Hassen"},{"idMembre":7,"nomMembre":"Hassen"},{"idMembre":8,"nomMembre":"Dakhl"},{"idMembre":9,"nomMembre":null},{"idMembre":10,"nomMembre":null},{"idMembre":11,"nomMembre":"AAAAAAAAAAAAA"},{"idMembre":12,"nomMembre":"BBBBBBbbbbbbb"},{"idMembre":13,"nomMembre":"CCCCCCCCCCCCC"},{"idMembre":14,"nomMembre":"jjjjjjjjj"},{"idMembre":15,"nomMembre":"uuuuuuuuuuuu"}]

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

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,
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

goodluck
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100