将数据发送到php服务器

时间:2014-03-20 16:35:41

标签: java php android

我想做的是将数据发送到php服务器但不访问该网站 或者使用浏览器。
通常在浏览器中可能会出现这样的想法:

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

我想使用Android(Java)将name1 = value1& name2 = value2发送到服务器。

POST或GET都适合我。

3 个答案:

答案 0 :(得分:0)

看看HttpRequest: http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

在教程中,其中:key1和value1的作用相同:$ _POST [' key1'] ==" value1"。

此外,您还需要使用AsyncTask,因为您无法在UI线程上运行网络任务。

答案 1 :(得分:0)

你可以通过以下方式实现这一目标:

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

不要忘记将<uses-permission android:name="android.permission.INTERNET" />添加到您的清单中。

答案 2 :(得分:0)

最简单的方法是使用HttpURLConnection(有关详情,请参阅this article)。

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();
    }
}