我的意思是我想向我的服务器发送一个http请求,比如:
http://blahblah.blah/index.php?命令=使
安卓系统

最佳答案

你可以这样做:
HTTP POST:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://blahblah.blah/index.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("command", "make"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

http get
HttpResponse response = null;
try {
        // Create http client object to send request to server
        HttpClient client = new DefaultHttpClient();
        // Create URL string
        String URL = "http://blahblah.blah/index.php?command=make";
        // Create Request to server and get response
        HttpGet httpget= new HttpGet();
        httpget.setURI(new URI(URL));
        response = client.execute(httpget);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

10-08 08:01
查看更多