zwmmenghuan123 2015-05-09 15:40 采纳率: 33.3%
浏览 1561

【求助万能的csdn】android客户端传递一个类到服务器端的问题?

现在是希望将android客户端生成的Device类传到服务器端处理,Device类已经实现了serializable类。
android端:

public static void upload(String url_path, Device d) {
try {
URL url = new URL(url_path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setReadTimeout(2000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
//GZIPOutputStream gzipout = new GZIPOutputStream(out);
ObjectOutputStream O_out = new ObjectOutputStream(out);
O_out.writeObject(d);
System.out.println(url_path + d.getDescribes()); //此处可以输出,可是
//gzipout.finish(); //服务器端没有收到请求。
O_out.flush();
O_out.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

url路径没问题。

服务器端:
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("--post--");
    InputStream in = request.getInputStream();
    //GZIPInputStream Gin = new GZIPInputStream(in);
    ObjectInputStream O_in = new ObjectInputStream(in);
    try {
        Device device = (Device) O_in.readObject();
        List<Object> list = new ArrayList<Object>();
        list.add(device.getId());
        list.add(device.getType());
        list.add(device.getSize());
        list.add(device.getDescribes());
        String sql = "insert into device values(?,?,?,?)";
        if(ju.updateByPreparedStatement(sql, list)) {
            System.out.println("上传完成!");
        }
    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("上传错误");
    }
}

如果有请求,服务器端应该输出“--post--”,很明显服务器没有收到android的请求
请问这代码错在哪呢?还有要如何实现类的传输?这段代码对吗?谢谢!
  • 写回答

4条回答 默认 最新

  • foreach_break 2015-05-09 15:50
    关注

    response.getOutputStream.write("--post--");

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料