日期:2014-05-20  浏览次数:20851 次

java 实现http post object功能
package sqlPost;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class SqlPost {
public void httpPost() throws IOException {
// Create Post String
// String data = URLEncoder.encode("myName", "UTF-8") + "="+ URLEncoder.encode("ttgg", "UTF-8");

AuctionItem item = new AuctionItem();
item.title = "Football";
item.days = 11;
item.startBid = (float) 1;
item.buyNowEnabled = true;
item.buyNowPrice = (float) 20;
item.deliveryCost = (float) 5;
item.description = "this football is a new one";

// Send Data To Page
URL url = new URL("http://localhost:9000/Application/show");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
// OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
oos.writeObject(item);
oos.flush();

// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
// you Can Break The String Down Here
}
}

public static void main(String args[]) throws Exception {
SqlPost formpost = new SqlPost();
formpost.httpPost();
}
}
-----------------------
出现的错误


Exception in thread "main" java.io.NotSerializableException: sqlPost.AuctionItem
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at sqlPost.SqlPost.httpPost(SqlPost.java:30)
at sqlPost.SqlPost.main(SqlPost.java:45)
是什么情况呢?O(∩_∩)O谢谢

------解决方案--------------------
AuctionItem 要实现Serializable接口