【问题标题】:HttpurlConnection Post Request body php server?HttpurlConnection 发布请求正文 php 服务器?
【发布时间】:2016-10-26 12:04:25
【问题描述】:

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

{"结果": "jfYRsbW17HA3bHtaJdDm", “errorMessage”:“错误”}

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

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



}

并在另一个活动中这样调用它:

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

【问题讨论】:

  • 帮助?............?
  • 可能你错过了标题或类似的东西(我也从你的 URL 收到“”)
  • 在其他程序上试试这个链接它工作正常:(
  • 我仍然从您的 URL 收到“”方法 = get_random_wallpaper。可能需要其他设置吗?
  • my key = method , value = get_random_wallpaper 和服务器 100% 工作,我的后端同事对其进行了测试

标签: php android http-post httpurlconnection


【解决方案1】:

您以错误的方式传递参数:您应该将 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();
                ...

【讨论】:

猜你喜欢
  • 2016-10-26
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 2017-10-06
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
相关资源
最近更新 更多