公司给了一个web service接口 ,只给了一个url ,并告诉是post请求, 以前没调用过 ,求大神们给个例子什么的。接口返回是json格式 我用from表单action提交那个url 结果是让我下载一个.json的文件,下载下来打开就是我想获得的json数据,这个接口该怎么调用啊。
直接发生http post请求。返回结果就是json数据
参数格式,也要告诉你的。
比如我是用jquery的ajax函数:
<script type="text/javascript">
function get_prov() {
$.ajax({
type: "post",
url: "page/basic_data/inst_apply/GetProv.action",
dataType: "json",
//data: {"userId": userId},
success: function(data) {
var d = eval("(" + data + ")");
for (var i = 0; i < d.length; i++) {
var id = d[i].addressCode;
var name = d[i].addressName;
var opt = "<option value='" + id + "'>" + name + "</option>";
$("#provId").append(opt);
}
},
error: function() {
alert("<%=SysTextConfig.getConfigInfo("sys_err_1")%>");
}
});
}
$(document).ready(function() {
get_prov();
});
像这样的你看下可不可以用
public void getList(){
list.clear();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpClient httpClient=new DefaultHttpClient();
HttpPost post=new HttpPost(MainActivity.path+"ChoiceSer");
HttpResponse response=httpClient.execute(post);
if(response.getStatusLine().getStatusCode()==200){
//获取jsonarray
HttpEntity entity=response.getEntity();
JSONArray array=new JSONArray(EntityUtils.toString(entity));
JSONObject object=null;
for (int i = 0; i < array.length(); i++) {
object=array.getJSONObject(i);
//分装类
Can can=new Can();
//通过object.get("key").toString()获取响应键的值
can.setId(Integer.valueOf(object.get("id").toString()));
can.setSubject(object.get("subject").toString());
right=object.get("Translation1").toString();
can.setTranslation1(right);
can.setTranslation2(object.get("Translation2").toString());
can.setTranslation3(object.get("Translation3").toString());
can.setTranslation4(object.get("Translation4").toString());
list.add(can);
}
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
提供给你我写的片段代码。
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
// 执行远程call调用
Client client = ClientBuilder.newClient();
WebTarget call_target = client.target(callUrl);
Response response = call_target.request(MediaType.APPLICATION_JSON).post(
Entity.entity(excuteCallRequestJson, MediaType.APPLICATION_JSON));
// 获取响应Code
int responseCode = response.getStatus();
// 如果响应成功,则获取响应Json内容,并且将执行结果置成true
if (responseCode == 200) {
String excuteRespJson = response.readEntity(String.class);
context.setExcuteRespJson(excuteRespJson);
System.out.println("excute responseJson=====" + context.getExcuteRespJson());
}