我有一个MVC控制器,它将User对象作为输入。这部署在Tomcat服务器上。 我用java类Client测试这个方法,用户填充在服务器上,我看到了预期的结果。 但是当我在Android应用程序中运行相同的客户端代码时,mvc方法testSingleObjectIn只是忽略了请求。 代码如下。 MVC控制器:
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpRequest;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dn.entity.User;
import com.dn.entity.UserInfo;
@Controller
public class MVCControllerTest {
@RequestMapping(value= "/testSingleObjectIn", method = RequestMethod.POST, headers="Accept=application/json")
@ResponseBody
public void testSingleObjectIn(@RequestBody User testObj) {
System.out.println("User ="+testObj.toString());
}
}
客户端在eclipse中作为Java应用程序执行:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.map.ObjectMapper;
import com.dn.entity.Region;
import com.dn.entity.User;
import com.dn.enums.RegionType;
import com.dn.enums.Salutation;
public class Client {
public static void main(String[] args) {
Client client = new Client();
client.testSingleObjectInput();
}
private void testSingleObjectInput(){
HttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
String url = "http://localhost:8080/DNServer/testSingleObjectIn";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
User user = new User();
user.setFirstName("XYZ");
user.setSurName("Abc");
user.setSalutation(Salutation.Mr);
ObjectMapper mapper = new ObjectMapper();
System.out.println( mapper.writeValueAsString(user));
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(user)));
HttpResponse response = httpClient.execute(httpPost);
System.out.println("testSingleObjectInput done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Android AsyncTask:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.map.ObjectMapper;
import android.os.AsyncTask;
import com.dn.entity.User;
public class RegistrationTask extends AsyncTask<User, Integer, Boolean> {
@Override
protected Boolean doInBackground(User... users) {
try {
HttpClient httpClient = new DefaultHttpClient();
String url = "http://10.0.2.2:8080/DNServer/testSingleObjectIn";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
ObjectMapper mapper = new ObjectMapper();
System.out.println( mapper.writeValueAsString(users[0]));
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(users[0])));
HttpResponse response = httpClient.execute(httpPost);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
用户VO:
import java.io.Serializable;
import com.dn.enums.Salutation;
public class User实现Serializable {
long userId;
String firstName;
String surName;
Salutation salutation;
public User() {
}
public User(long userId, String firstName, String surName, Salutation salutation) {
super();
this.userId = userId;
this.firstName = firstName;
this.surName = surName;
this.salutation = salutation;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public Salutation getSalutation() {
return salutation;
}
public void setSalutation(Salutation salutation) {
this.salutation = salutation;
}
@Override
public String toString() {
return salutation.toString()+" firstName ="+ firstName+" LastName= "+surName;
}
}