上一篇学习了fitness的安装,这一篇将会来学习fitness与后台的简单交互
编辑test page页面
后台项目结构如下
这里的HttpInto对应的就是后台代码的HttpInto这个类,该类的代码如下
import org.apache.http.client.methods.CloseableHttpResponse;
import java.io.IOException;
import java.util.HashMap;
public class HttpInto {
String url;
String entityString;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getEntityString() {
return entityString;
}
public void setEntityString(String entityString) {
this.entityString = entityString;
}
public int getCode() throws IOException {
CloseableHttpResponse closeableHttpResponse;
HashMap<String, String> headermap = new HashMap<String, String>();
headermap.put("Content-Type", "application/json");
headermap.put("Accept-Charset", "UTF-8");
closeableHttpResponse = HttpClient1.post(url,entityString,headermap);
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
return statusCode;
}
}
HttpClient1类代码如下
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class HttpClient1 {
final static Logger Log = Logger.getLogger(HttpClient1.class);
public static CloseableHttpResponse post(String url, String entityString, HashMap<String,String> headermap) throws ClientProtocolException, IOException {
//创建一个可关闭的httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建一个HttpPost的请求对象
HttpPost httppost = new HttpPost(url);
//将参入传入(可以组织成自己想要的任何形式)
httppost.setEntity(new StringEntity(entityString));
//加载请求头到httppost对象
for(Map.Entry<String, String> entry : headermap.entrySet()) {
httppost.addHeader(entry.getKey(), entry.getValue());
}
//发送post请求(httppost对象包含了url,请求头和参数)
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
Log.info("开始发送post请求");
int statusCode = httpResponse.getStatusLine().getStatusCode();
return httpResponse;
}
}
这里借鉴了我很喜欢的一个作者的代码,他的博客地址是https://me.csdn.net/u011541946,在他那儿能学到很多东西哦。
url和entityString对应该类的set方法传入的值,getcode?代表getCode方法返回的值,fitness会将返回的值与getcode?对比,相等就会显示通过
fitness编辑后点击保存的页面如下
然后我们来编写对应的设置,点击root
编辑如下,保存
这里的path是我项目打包之后的路劲,保存之后点击test
然后我们就成功啦