如何通过POST方法将数据从Android发送到php?

时间:2018-05-01 14:26:59

标签: android

我需要通过POST方法将这些数据发送到PHP页面我已经尝试了几个例子,但没有任何喜悦,任何人都可以给我一个简单的例子。

我可以通过GET方法成功发送。

String serial = android.os.Build.SERIAL;
String board = android.os.Build.BOARD;
String display = android.os.Build.DISPLAY;
String model = Build.MODEL;
String id = android.os.Build.ID;
String manufacturer = Build.MANUFACTURER;
String brand = Build.BRAND;
String type = Build.TYPE;
String user = Build.USER;
String base = "" + android.os.Build.VERSION_CODES.BASE;
String incremental = android.os.Build.VERSION.INCREMENTAL;
String sdk = android.os.Build.VERSION.SDK;
String host = Build.HOST;
String fingerprint = Build.FINGERPRINT;
String versionCode = Build.VERSION.RELEASE;

android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_VIEW,
android.net.Uri.parse("http://xxxxxxxxxxxx/?Serial="+serial+"&Board="+board+"" +
        "&Display="+display+"&Model="+model+"&Id="+id+"&Manufacture="+manufacturer+"&Brand="+brand+"" +
        "&Type="+type+"&User="+user+"&Base="+base+"&Incremental="+incremental+"&SDK="+sdk+"" +
        "&Host="+host+"&Fingerprint="+fingerprint+"&VersionCode="+versionCode));
startActivity(intent);

1 个答案:

答案 0 :(得分:1)

您可以使用Android的 $scope.exportData = function () { var mystyle = { headers: true, column: { style: { Font: { Bold: "1" } } }, rows: { style: { Font: { Color: "#fff" } } }, cells: { style: { Font: { Color: "#00FFFF" } } } }; $scope.getFilteredColumnsForExcel = "Some Data"; alasql('SELECT * INTO XLSXML("test.xls",?) FROM ?', [mystyle, $scope.getFilteredColumnsForExcel]); }; 课程。以下是使用参数发出请求的方法。

HttpURLConnection

传递给此方法的参数应进行编码,以避免因特殊字符造成的任何不便。以下是编码参数的方法

// Create Post Request
public String makePostRequest(String requestURL, String params) {

    String text = "";
    BufferedReader reader = null;

    // Send data
    try {
        // Defined URL  where to send data
        URL url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept", "*/*");

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(params);
        wr.flush();

        // Get the server response

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            // Append server response in string
            sb.append(line + "\n");
        }

        text = sb.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // Show response on activity
    return text;
}

为了发出Http请求,请按以下方式调用这些方法:

public String encodeParams(HashMap<String, String> params) throws UnsupportedEncodingException {
    String encodedParams = "";
    /* Display content using Iterator*/
    Set set = params.entrySet();
    Iterator iterator = set.iterator();
    while (iterator.hasNext()) {
        if (encodedParams != null && !encodedParams.equals("")))
            encodedParams += "&";
        Map.Entry mentry = (Map.Entry) iterator.next();
        encodedParams += URLEncoder.encode(mentry.getKey().toString(), "UTF-8")
                + "=" + URLEncoder.encode(mentry.getValue().toString(), "UTF-8");
    }
    return encodedParams;
}

P.S。您应该尝试使用后台线程或try { // prepare parameters for http call HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("serial", android.os.Build.SERIAL); hashMap.put("board", android.os.Build.BOARD); // encode parameters String encodedParams = encodeParams(hashMap); // create request url String requestUrl = "http://xxxxxxxxxxxx/"; // your URL // make a post request to server with request url create above String response = makePostRequest(requestUrl, encodedParams, null); return response; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } 运行此代码,以获得更好的性能,避免AsyncTask异常。