Php 如何从Android发送http post请求?

Php 如何从Android发送http post请求?,php,android,http-post,Php,Android,Http Post,我是android新手,现在我已经厌倦了搜索关于android HTTP请求的最佳教程 我正试图将这些参数发送到phpServer Name=“一些名字” email=“一些电子邮件” 我们可以在MainActivity.java文件中执行此操作吗?这需要任何jar(库)文件吗? 欢迎提出任何建议 我试过这个 @Override public void onClick(View view) { if(view == btnSubscribe){ HttpURLConn

我是android新手,现在我已经厌倦了搜索关于android HTTP请求的最佳教程

我正试图将这些参数发送到phpServer

  • Name=“一些名字”

  • email=“一些电子邮件”

我们可以在MainActivity.java文件中执行此操作吗?这需要任何jar(库)文件吗? 欢迎提出任何建议

我试过这个

@Override
public void onClick(View view) {
    if(view == btnSubscribe){
        HttpURLConnection client = null;
        try {
            URL url = new URL("http://www/somewebsite.com/API/SUBSCRIBE");
            client = (HttpURLConnection) url.openConnection();
            client.setRequestMethod("POST");
            client.setRequestProperty("name","test one");
            client.setRequestProperty("email","text@gmail.com");
            client.setDoOutput(true);

            OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
            outputPost.flush();
            outputPost.close();
            client.setChunkedStreamingMode(0);

        } catch(MalformedURLException error) {
            showError("Handles an incorrectly entered UR");
        }
        catch(SocketTimeoutException error) {
            showError("Handles URL access timeout");
        }
        catch (IOException error) {
            showError("Handles input and output errors");
        }finally {
            if(client != null)
            client.disconnect();
        }
    }
}

首先,大多数httpClint现在已被弃用,因此如果您现在可以使用httpUrlConnection进行相同的操作

你可以参考

若你们想使用图书馆,那个么你们可以使用

这两个是最好的图书馆


干杯

首先,大多数httpClint现在已被弃用,因此如果您现在可以使用httpUrlConnection进行相同的操作

你可以参考

若你们想使用图书馆,那个么你们可以使用

这两个是最好的图书馆


干杯

我正在使用此代码,并且也在工作。试试这个代码

public static String httpPostRequest(Context context, String url, String email) {
        String response = "";
        BufferedReader reader = null;
        HttpURLConnection conn = null;
        try {
            LogUtils.d("RequestManager", url + " ");
            LogUtils.e("data::", " " + data);
            URL urlObj = new URL(url);

            conn = (HttpURLConnection) urlObj.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            data += "&" + URLEncoder.encode("Email", "UTF-8") + "="
                    + URLEncoder.encode(email, "UTF-8");


            wr.write(data);
            wr.flush();

            LogUtils.d("post response code", conn.getResponseCode() + " ");

            int responseCode = conn.getResponseCode();



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

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            response = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.d("Error", "error");
        } finally {
            try {
                reader.close();
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception ex) {
            }
        }
        LogUtils.d("RESPONSE POST", response);
        return response;
    }

我使用了这个代码,也在工作。试试这个代码

public static String httpPostRequest(Context context, String url, String email) {
        String response = "";
        BufferedReader reader = null;
        HttpURLConnection conn = null;
        try {
            LogUtils.d("RequestManager", url + " ");
            LogUtils.e("data::", " " + data);
            URL urlObj = new URL(url);

            conn = (HttpURLConnection) urlObj.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            data += "&" + URLEncoder.encode("Email", "UTF-8") + "="
                    + URLEncoder.encode(email, "UTF-8");


            wr.write(data);
            wr.flush();

            LogUtils.d("post response code", conn.getResponseCode() + " ");

            int responseCode = conn.getResponseCode();



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

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            response = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.d("Error", "error");
        } finally {
            try {
                reader.close();
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception ex) {
            }
        }
        LogUtils.d("RESPONSE POST", response);
        return response;
    }

@Sahan通过@IntelliJAmiya抱歉,我清除了所有代码,因为我不工作。现在我在做一个新项目。@Nisarg谢谢。@IntelliJAmiya各位,你们现在能看一下吗?我试过了。
name
应该是
name
@Sahan-Go-through@IntelliJAmiya抱歉,我清除了所有代码,因为我不工作。现在我在做一个新项目。@Nisarg谢谢。@IntelliJAmiya各位,你们现在能看一下吗?我试过了。
name
应该是
name