MHGetApiImp.java
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.hjx.miaohongentry.http;
import com.hjx.miaohongentry.bean.AppVersion;
import com.hjx.miaohongentry.builder.UpdateInfoBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* Created by l on 2017/7/1.
*/
public class MHGetApiImp implements MHGetApi {
private String doGet(String query) throws Exception {
String path = "http://boss.hjx.com"+ query;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
//获得结果码
int responseCode = connection.getResponseCode();
if(responseCode ==200){
//请求成功 获得返回的流
InputStream is = connection.getInputStream();
return InputStream2String(is);
}else {
//请求失败
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public AppVersion getUpdateInfo(String pid, String appName) throws Exception {
String jsonStr = doGet("/general/release/version?pid="+pid+"&appName="+appName);
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
UpdateInfoBuilder builder = new UpdateInfoBuilder();
return builder.build(jsonObject);
}
return null;
}
public static String InputStream2String(InputStream in) {
InputStreamReader reader = null;
try {
reader = new InputStreamReader(in, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = "";
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}