HttpurlConnection Post 请求主体 php 服务器?

HttpurlConnection Post Request body php server?

我有一个带有文件的服务器。php 我想用我的应用程序将 post request 发送到服务器我正在尝试这样做,但我总是得到“ “当我在 postman 上尝试我的服务器时,我得到

{"result": "jfYRsbW17HA3bHtaJdDm", "errorMessage": "error"}

我希望我的应用程序在我发送 Post 请求时看到那些是我的代码:(为什么我得到空值)

public class HttpParoonakRequest {


public static String HttpParoonakRequestGetUrl(){


    URL url ;
        HttpURLConnection client = null;
    try {

        url = new URL("myphp");

        client = (HttpURLConnection) url.openConnection();
       client.setRequestMethod("POST");

        client.setRequestProperty("method","get_random_wallpaper");



        client.setDoInput(true);
        client.setDoOutput(true);
        client.connect();
        DataOutputStream wr = new DataOutputStream (
                client.getOutputStream ());

        wr.flush ();
        wr.close ();

        //Get Response
        InputStream is = client.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();

        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\n');

        }
        Log.d("hey",response.toString());

        rd.close();

        return response.toString();


    } catch (Exception e) {

        e.printStackTrace();
        return e.getMessage();
    }
    finally {
        if(client != null) // Make sure the connection is not null.
            client.disconnect();
    }



}

并在另一个 activity:

中这样称呼它
 thread = new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    String abcd = HttpParoonakRequest.HttpParoonakRequestGetUrl();
                    System.out.println("Message1 "+ abcd);

                } catch (Exception e) {

                  e.printStackTrace();
                    e.getMessage();
               }

            }
        });
        thread.start();

您以错误的方式传递参数:您应该将 client.setRequestProperty("method","get_random_wallpaper") 替换为 client.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 并通过 wr 流在请求正文中传递您的参数,如下所示:

                ...    

                client = (HttpURLConnection) url.openConnection();
                client.setRequestMethod("POST");

                client.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                client.setDoInput(true);
                client.setDoOutput(true);
                client.connect();
                DataOutputStream wr = new DataOutputStream(
                        client.getOutputStream());

                wr.write("method=get_random_wallpaper".getBytes());

                wr.flush();
                wr.close();
                ...