我是使用Spring mvc4注释的新手。我想做的就是使用spring mvc作为Web服务。所以,如果有人能为我提供解决方案,我将感激不尽。
我的安卓代码如下:
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost( "http://localhost:8080/datarequest");
JSONObject json = new JSONObject();
json.put("action", "check_login");
json.put("username","name");
json.put("password", "password");
JSONArray postjson = new JSONArray();
postjson.put(json);
httppost.setHeader("json", json.toString());
httppost.getParams().setParameter("jsonpost", postjson);
System.out.println(postjson);
HttpResponse response = httpclient.execute(httppost);
到目前为止,我将网络服务编写为:
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/datarequest")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String getMethod(ModelMap model) {
System.out.println("GET");
return "hello";
}
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public String getMethod(ModelMap model) {
System.out.println("POST");
System.out.println("here i want to print json data send from the android ");
return "hello";
}
}
答案 0 :(得分:1)
您可以使用@RequestBody
并将其映射到所需的类结构。您可以参考此问题@RequestBody and @ReponseBody spring。
要进行测试,您可以将其映射到String。见下面的例子。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
您可以在pom中添加以下maven条目。
RadioButtonList