m7m
m7m

Reputation: 49

send data from Android app to local server

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

Upvotes: 2

Views: 4406

Answers (1)

Justin Thomas
Justin Thomas

Reputation: 5848

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

Upvotes: 4

Related Questions