将数据发送到php服务器[关闭]

What i would like to do, is to send data to a php server but without visiting the site or using a browser.
Normally in a browser it is somethink like this:

/test/index.php?name1=value1&name2=value2

I would like to send name1=value1&name2=value2 to the server using Android(Java).

Both POST or GET work for me.

You can achieve this through something like:

final HttpGet uri = new HttpGet("http://example.com/test/index.php?name1=value1");
final DefaultHttpClient httpClient = new DefaultHttpClient();
final HttpResponse response = httpClient.execute(uri);

Don't forget to add <uses-permission android:name="android.permission.INTERNET" /> to your manifest.

have a look at HttpRequest: http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

In tutorial where there is: key1 and value1 that acts same as: $_POST['key1'] == "value1".

Also you will need to use AsyncTask since you cannot run network tasks on UI thread.

Simplest approach would be using HttpURLConnection (see this article for more information).

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL url = new URL("/test/index.php?key="+ + URLEncoder.encode(value);

        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}