java请求数据并存入数据库

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.esen.gxdc.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import org.drools.lang.DRLParser.neg_operator_key_return;
import org.json.JSONArray;
import org.json.JSONObject;

import com.esen.gxdc.dao.ApiBikeInfoAllDao;
import com.esen.gxdc.dao.ApiBikeInfoDao;
import com.esen.gxdc.entity.ApiBikeInfo;
import com.google.gson.JsonArray;
import com.sun.xml.bind.unmarshaller.InfosetScanner;

public class ApiInfo2db {

private final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception {

ApiInfo2db http = new ApiInfo2db();

// System.out.println("Testing 1 - Send Http GET request");
http.sendGetBike();
http.sendGetCompany();
// System.out.println("\nTesting 2 - Send Http POST request");
// http.sendPost();

}

// HTTP GET请求
private void sendGetBike() throws Exception {

String url = "http://localhost:8888/ssm/getApiData.do";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//默认值我GET
con.setRequestMethod("GET");

//添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//打印结果
System.out.println(response.toString());
JSONArray jsonArray=new JSONArray(response.toString());
List<ApiBikeInfo>infos=new ArrayList<ApiBikeInfo>();
for(int i=0;i<jsonArray.length();i++){
ApiBikeInfo apiBikeInfo=new ApiBikeInfo();
apiBikeInfo.setId(jsonArray.getJSONObject(i).getString("Id"));
apiBikeInfo.setCompanyid(jsonArray.getJSONObject(i).getString("CompanyId"));
apiBikeInfo.setCode(jsonArray.getJSONObject(i).getString("Code"));
apiBikeInfo.setLicense(jsonArray.getJSONObject(i).getString("License"));
apiBikeInfo.setLaunchdate(jsonArray.getJSONObject(i).getString("LaunchDate"));
apiBikeInfo.setState(jsonArray.getJSONObject(i).getString("State"));
apiBikeInfo.setUpdatetime(jsonArray.getJSONObject(i).getString("UpdateTime"));
apiBikeInfo.setUploadtime(jsonArray.getJSONObject(i).getString("UploadTime"));
apiBikeInfo.setHistory(jsonArray.getJSONObject(i).getString("History"));
infos.add(apiBikeInfo);
ApiBikeInfoDao.getInstance().add(apiBikeInfo);
}


}
// HTTP GET请求
private void sendGetCompany() throws Exception {

String url = "http://localhost:8888/ssm/getApiData.do";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//默认值我GET
con.setRequestMethod("GET");

//添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//打印结果
System.out.println(response.toString());
JSONArray jsonArray=new JSONArray(response.toString());
List<ApiBikeInfo>infos=new ArrayList<ApiBikeInfo>();
for(int i=0;i<jsonArray.length();i++){
ApiBikeInfo apiBikeInfo=new ApiBikeInfo();
apiBikeInfo.setId(jsonArray.getJSONObject(i).getString("Id"));
apiBikeInfo.setCompanyid(jsonArray.getJSONObject(i).getString("CompanyId"));
apiBikeInfo.setCode(jsonArray.getJSONObject(i).getString("Code"));
apiBikeInfo.setLicense(jsonArray.getJSONObject(i).getString("License"));
apiBikeInfo.setLaunchdate(jsonArray.getJSONObject(i).getString("LaunchDate"));
apiBikeInfo.setState(jsonArray.getJSONObject(i).getString("State"));
apiBikeInfo.setUpdatetime(jsonArray.getJSONObject(i).getString("UpdateTime"));
apiBikeInfo.setUploadtime(jsonArray.getJSONObject(i).getString("UploadTime"));
apiBikeInfo.setHistory(jsonArray.getJSONObject(i).getString("History"));
infos.add(apiBikeInfo);
ApiBikeInfoDao.getInstance().add(apiBikeInfo);
}


}

// HTTP POST请求
private void sendPost() throws Exception {

String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//添加请求头
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

//发送Post请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//打印结果
System.out.println(response.toString());

}

}

`xml