【问题标题】:HttpURLConnectoin POST data not sent未发送 HttpURLConnectoin POST 数据
【发布时间】:2013-07-23 11:00:43
【问题描述】:

我想从 Android 应用程序向我的网络服务器发送 json 数据。因此我写了以下方法:

@Override
    protected String doInBackground(String... params) {
        String id = Configuration.getUserId();
        String date = sdf.format( new Date() );
        String type = "message";
        String message = params[0];

        try {

            JSONObject json = new JSONObject();
            json.put( "id", id);
            json.put( "date", date );
            json.put( "type", type );
            json.put( "message", message );

            byte[] postData = URLEncoder.encode( json.toString(), "UTF-8" ).getBytes();

            URL url = new URL( Configuration.loggingURL );
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            //conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
            conn.setUseCaches(false);

            DataOutputStream out = new DataOutputStream ( conn.getOutputStream () );
            out.write(postData);
            out.flush();
            out.close();

            conn.disconnect();

        } catch( Exception e ) {
            e.printStackTrace();
        }

        Log.d("RemoteLogging","Data sent.");

        return null;
    }

在服务器端我有这样的东西:

public class LoggingServlet extends HttpServlet {


    private static final long serialVersionUID = 2L;

    @Override
    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
    {
        System.out.println("LoggingServlet: doPost got called");

但是,这不起作用,没有数据到达我的服务器。 doPost 方法甚至没有被调用(即我在我的 tomcat 服务器的日志中什么也看不到)。我还有一个 iPhone 应用程序可以成功地将数据发送到我的服务器,并且我在浏览器中测试了 Configuration.loggingURL 以便我可以确定它是正确的调用 URL。

我在这里做错了什么?

更新:我刚刚检查了 Wireshark,没有来自 Android 应用程序的传出流量(在模拟器中运行)。所以什么都没有发送。

【问题讨论】:

    标签: android http-post httpurlconnection


    【解决方案1】:

    我依稀记得过去偶然发现了这个问题。据我所知,罪魁祸首是保持活跃的连接。

    尝试设置System.setProperty("http.keepAlive", "false");,然后发出请求。

    【讨论】:

    • 感谢您的回答。我添加了 System.setProperty("http.keepAlive", "false");上面的 URL url = new URL( Configuration.loggingURL );但它没有效果。
    • 嗯,尝试在一切之前设置它,例如在Application.onCreate()。如果不起作用,则可能不是这种情况。无论如何,HttpUrlConnection 在 HttpClient 会更好。不过,我会在你的情况下使用 come 库,VolleyAndroid AsyncHttpClient
    【解决方案2】:

    我发现了如何解决它,但不明白为什么它现在可以工作:

    我换了

    byte[] postData = URLEncoder.encode( json.toString(), "UTF-8" ).getBytes();
    
            URL url = new URL( Configuration.loggingURL );
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            //conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
            conn.setUseCaches(false);
    
            DataOutputStream out = new DataOutputStream ( conn.getOutputStream () );
            out.write(postData);
            out.flush();
            out.close();
    
            conn.disconnect();
    

    使用以下代码:

    URL url = new URL( Configuration.loggingURL );
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                conn.setRequestProperty("Content-Length", Integer.toString( json.toString().length() ));
                conn.setFixedLengthStreamingMode( json.toString().length() );
                conn.setUseCaches(false);
                conn.connect();
    
                OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
                writer.write( json.toString() );
                writer.flush();
                conn.disconnect();
    

    这很好用。

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 2013-08-24
      相关资源
      最近更新 更多