HttpURLConnection POST数据未发送

时间:2013-07-23 11:00:44

标签: android http-post httpurlconnection

我想从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应用程序没有传出流量(在模拟器中运行)。所以没有发送任何东西。

2 个答案:

答案 0 :(得分:0)

我依稀记得曾经在这个问题上磕磕绊绊。据我所记,罪魁祸首就是保持活着的关系。

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

答案 1 :(得分:0)

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

我替换了

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

这很好用。