2

i want to send data from android application to local server (PHP) but it doesn't work this is my code (it is work with remote server ):

        String path ="http://localhost/sd.php";
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);

        HttpResponse response;

        JSONObject json = new JSONObject();
        try {
            HttpPost post = new HttpPost(path);

            json.put("im", 999);
            json.put("cTime", 12);
            Log.i("jason Object", json.toString());
            post.setHeader("json", json.toString());
            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
            post.setEntity(se);

            response = client.execute(post);


        } catch (Exception e) {
            Object n=e.getStackTrace();
            Toast.makeText( getApplicationContext(),n.toString(),Toast.LENGTH_SHORT).show();
        } 

i thing the wrong in the address http://localhost/sd.php pleas help me to find a solution to this problem

1
  • I guess by "local" you mean "on the emulator's host machine"... That won't be "local" on a real phone. Commented Mar 27, 2011 at 19:16

1 Answer 1

4

You need the IP address of the server. Localhost from the app is the phone.

Sign up to request clarification or add additional context in comments.

2 Comments

How can i get the IP address of the server(it is local server)?
On the server type ifconfig if using a Mac or Unix box. ipconfig in Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.