JAVA WEB接口开发简述

目录

1. JAVA WEB接口开发简述

1.1. 基本了解

1.2. 提供接口

1.3. 调用接口

1. JAVA WEB接口开发简述

1.1. 基本了解

  当我们想去访问其他网站的接口时候,而又不想要登录验证等操作,那么可以选择使用平台提供的接口进行调用得到我们需要的信息。比如说,一个网站A有一个接口路径: http://192.168.10.119:8080/xmq/webservice/menu/search?ak=APP00013&token=yq6ZaljwYMa1x83r0hSHVhQ45DA%3D

  当我们需要调用这个接口的时候就要满足ak参数以及token参数。这个时候,我们需要去拼接这样的一个url,然后调用平台提供的jar包或者其他的工具去获取信息。

1.2. 提供接口

  确保网站A提供的调用接口可以使用,这里开发接口的时候,需要定义一些规则,比如具体的返回数据定义,状态码定义等等,以便调用更明了。具体开发要根据实际情况来决定。

1.3. 调用接口

  这样的接口我们可能用到这些jar包,如下图:

   

  当然还有json等相关的jar包,这个需要根据调用的网站来确定需要哪些具体的jar包。

  常用到的类如HttpClientHttpGetHttpPostHttpDelete等。

  简单调用HttpGet

 

1 protected HttpClient c;
2 
3 HttpGet get = new HttpGet(url);
4 
5 HttpResponse response = c.execute(get);

 

  简单调用HttpPost

1 HttpPost post = new HttpPost(url);
2 
3 StringEntity entity = new StringEntity(json, ContentType.create("text/plain", "UTF-8"));
4 
5 post.setEntity(entity);
6 
7 response = c.execute(post);

 

  简单调用HttpDelete

1 HttpDelete delete = new HttpDelete(url);
2 
3 HttpResponse response = c.execute(delete);

  调用文件流和参数(比如有的接口是文件上传的情况):

 

//定义调用参数
String private_key = "fcea920f7412b5da7be0cf42b8c93759";
String timestampP=getTimestamp()+"";
String accountP = "123456";
String projidP = "10000";
String typeP = "1";
String signP=accountP+private_key+timestampP+projidP+typeP;

//设置请求参数
StringBody account = new StringBody(accountP);
StringBody timestamp = new StringBody(timestampP);
StringBody sign = new StringBody(getMD5Str(signP));
StringBody projid = new StringBody(projidP);
StringBody type = new StringBody(typeP);
FileBody fb = new FileBody(new File("C:/xmq.dwg"));

//设置请求参数类型
MultipartEntity reqEntity = new MultipartEntity();

reqEntity.addPart("account", account);
reqEntity.addPart("timestamp", timestamp);
reqEntity.addPart("projid", projid);
reqEntity.addPart("sign", sign);
reqEntity.addPart("type", type);
reqEntity.addPart("file", fb);

String url = "http://122.22.11.50:9090/UploadFile";
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(reqEntity);
BasicHttpParams httpParams = new BasicHttpParams();  
HttpConnectionParams.setConnectionTimeout(httpParams, 120000);//设置连接超时  
HttpConnectionParams.setSoTimeout(httpParams, 120000);//设置请求超时 HttpClient client
= new DefaultHttpClient(); HttpResponse response = client.execute(httpPost); System.out.println("调用结果Data:" + CameraUtil.getData(response));
//附件上传接口,需要注意接口提供者的服务器有没有限制文件上传的大小、请求超时的时间等;
//我就碰到这样的情况,大附件怎么都上传不了,我找了很久原因,发现接口提供者IIS服务限制了附件上传的大小。。。

 

调用文件流(比如有的接口是文件下载的情况):

 

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class FileDown {

    public static void main(String[] args) {
        // 根据已有的url连接下载文件
        String urlStr = "http://117.27.145.20:8088/2017010915281017322.edc";
        String fileName = "C:/Users/Administrator/Desktop/work/wjxt_文件系统/a.edc";
        downloadFromUrl(urlStr, fileName);
    }

    /**
     * 根据urlStr下载流,下载文件到指定fileName
     * 
     * @param urlStr
     * @param fileName
     */
    public static void downloadFromUrl(String urlStr, String fileName) {
        // 构造URL
        URL url;
        try {
            url = new URL(urlStr);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            InputStream is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流s
            OutputStream os = new FileOutputStream(new File(fileName));
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            // 完毕,关闭所有链接
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

 

 

 

 

 

  我们可以对response进行处理,如:

 1 //200为成功状态码
 2 
 3 if(response.getStatusLine().getStatusCode() == 200){
 4 
 5   String responseText = null;
 6 
 7   try {
 8 
 9     responseText = EntityUtils.toString(response.getEntity() , "UTF-8");
10 
11   } catch (ParseException e) {
12 
13     e.getMessage();
14 
15   } catch (IOException e) {
16 
17      e.getMessage();
18 
19   }
20 
21   //返回数据处理responseText
22 
23   //一般是json数据格式,根据实际需求处理
24 
25 }else{
26 
27   //异常信息response.getStatusLine().getStatusCode();
28 
29 }

 

posted @ 2017-01-24 10:52  迢迢  阅读(29173)  评论(2编辑  收藏  举报