1.今天有个需求是在android客户端用HttpClient,HttpPost HttpGet HttpResponse发送请求并获取服务器端的数据,服务端是struts2框架,
解决方案一:
struts2对Json的支持
解决方案二:
1:在action中以流的形式输出响应,
编写action:
public class TestAction {
private InputStream imageStream; //这个流就是要输出给用户的流,流里边的内容将显示给用户,相当于response.getOutPutStream()
public InputStream getImageStream() {
return imageStream;
}
public void setImageStream(InputStream imageStream) {
this.imageStream = imageStream;
}
public String execute() {
System.out.println("-------------");
iStream= new ByteArrayInputStream("outputsomething".getBytes()); //对流赋值,此值久是想输出的内容
return "success";
}
}
2:<action name="xx" class="test.TestAction" method="execute">
<result name="success" type="stream"> //结果类型配置成流的形式
<param name="inputName">imageStream</param>//把自己写的inputstream赋值结果类型中的inputName
</result>
</action>
输出结果
android 客户端部分代码:
HttpClient hc = new DefaultHttpClient();
HttpPost get = new HttpPost("http://192.168.2.189:8080/SecTrad/xx");
try {
HttpResponse response = hc.execute(get);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
String value = EntityUtils.toString(entity, "utf-8");
System.out.println(value);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果在action中用到servletactioncontext.getresponse.getoutputstream();会影响到框架,因为结果类型会是dispatcher用到了outputstream