在开始之前说些题外话,本文章为本菜鸟的第一篇,有不足之处请高手指点,谢谢!
转入正题:在实际的开发运用过程中,android的通信会用到httpClient来传输数据,在我之前的项目中都是用如下形式传输:
NameValuePair nvp1 = new BasicNameValuePair("username", name);
NameValuePair nvp2 = new BasicNameValuePair("password", age);
List<NameValuePair> list = new ArrayList<NameValuePair>();
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(list,HTTP.UTF_8);
httpPost.setEntity(requestHttpEntity);
然而我发现这其中只能传一些String类型的,在BasicNameValuePair中并不能用键值对形式的去传输一个对象类型的数据如:
Book book = new Book();
NameValuePair nvp1 = new BasicNameValuePair("book", book);这样是不可以行的。那我要传输对象怎么办了?在查看API后发现可以运用SerializableEntity来传送对象,下面我就上代码:
实体类Book.class
package com.gutao.entity;
import java.io.Serializable;
public class Book implements Serializable {
private String name;
private String price;
private String author;
public Book() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Activity如下:
public class MainActivity extends Activity {
private Button postButton;
private String baseUrl = "http://192.168.31.23:8080/Wireless_server/user/login.do";
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
postButton = (Button) findViewById(R.id.buttonPost);
postButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Book b = new Book();
b.setName("android高级编程");
b.setAuthor("XXXX");
b.setPrice("65$");
try {
HttpEntity entity = new SerializableEntity(b, true);
HttpPost httpPost = new HttpPost(baseUrl);
httpPost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
InputStream in = httpEntity.getContent();
String line = "";
String result = "";
BufferedReader bfr = new BufferedReader(
new InputStreamReader(in));
while ((line = bfr.readLine()) != null) {
result += line;
}
Log.i("Post", result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
记得加入网络权限哦!
<uses-permission android:name="android.permission.INTERNET" />
当点击postButton的时候将传输数据。后台程序为:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView post(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("=============Post");
try {
request.setCharacterEncoding("utf-8");
ObjectInputStream dis = new ObjectInputStream(
request.getInputStream());
Book book = (Book) dis.readObject();
System.out.println(book.getName() + "" + book.getPrice());
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ModelAndView mav = new ModelAndView("post");
List<String> list = new ArrayList<String>();
list.add("hehe");
list.add("Hello");
mav.addObject("name", list);
return mav;
}
基本就是这样,不足之处,欢迎批评指正!谢谢!