我android studio导入别人demo,直接run成功后为什么打开代码还会有import错误,关键不理会错误直接run还是能成功╮( ̄▽ ̄")╭
package com.hand.demo;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
public class ItemQuery {
private static String reportServiceUrl = "https://efcw.bi.ap4.oraclecloud.com/xmlpserver/services/v2/ReportService";
private static String uid = "tn.webservice";
private static String pwd = "Welcome123";
public static String httpPost(String destUrl, String postData, String authStr, String soapAction) throws Exception {
String response = "";
URL url = new URL(destUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn == null) {
return null;
}
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("SOAPAction", soapAction);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setFollowRedirects(true);
conn.setAllowUserInteraction(false);
conn.setRequestMethod("POST");
System.out.println("==================Service request headers: ================ ");
Map<String, List<String>> requestHeaders = conn.getRequestProperties();
for (String k : requestHeaders.keySet()) {
System.out.println(k + ":" + requestHeaders.get(k));
}
byte[] authBytes = authStr.getBytes("UTF-8");
String auth = new String(Base64.getEncoder().encode(authBytes));
conn.setRequestProperty("Authorization", "Basic " + auth);
System.out.println("==================Service request body: ================ ");
System.out.println(postData);
OutputStream out = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(postData);
writer.close();
out.close();
System.out.println("==================Service response: ================ ");
System.out.println(
"connection status: " + conn.getResponseCode() + "; connection response: " + conn.getResponseMessage());
System.out.println("==================Service response headers: ================ ");
for (int i = 0;; i++) {
String mine = conn.getHeaderField(i);
if (mine == null)
break;
System.out.println(conn.getHeaderFieldKey(i) + ":" + mine);
}
int respCode = conn.getResponseCode();
if (respCode != 200) {
InputStream ei = conn.getErrorStream();
response = readGzipStr(ei);
ei.close();
return response;
}
InputStream in = conn.getInputStream();
InputStreamReader iReader = new InputStreamReader(in);
BufferedReader bReader = new BufferedReader(iReader);
String line;
System.out.println("==================Service response: ================ ");
while ((line = bReader.readLine()) != null) {
response += line;
}
iReader.close();
bReader.close();
in.close();
conn.disconnect();
return response;
}
public static String readGzipStr(InputStream in) {
String str = "";
GZIPInputStream gunzip = null;
try {
gunzip = new GZIPInputStream(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = gunzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
str = out.toString();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (gunzip != null) {
try {
gunzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str.toString();
}
public static String queryItemData() {
String payTemplate = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v2=\"http://xmlns.oracle.com/oxp/service/v2\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>" + " <v2:runReport>"
+ " <v2:reportRequest>"
+ " <v2:attributeCalendar>Gregorian</v2:attributeCalendar>"
+ " <v2:attributeFormat>xml</v2:attributeFormat>"
+ " <v2:attributeLocale>en_US</v2:attributeLocale>"
+ " <v2:attributeUILocale>en_US</v2:attributeUILocale>"
+ " <v2:attributeTimezone>China/Hong_Kong</v2:attributeTimezone>"
+ " <v2:parameterNameValues>"
+ " <v2:listOfParamNameValues>"
+ " <v2:item>"
+ " <v2:dataType>String</v2:dataType>"
+ " <v2:name>P_LIMIT</v2:name>"
+ " <v2:values>"
+ " <v2:item>10</v2:item>"
+ " </v2:values>"
+ " </v2:item>"
+ " <v2:item>"
+ " <v2:dataType>String</v2:dataType>"
+ " <v2:name>P_PAGE</v2:name>"
+ " <v2:values>"
+ " <v2:item>1</v2:item>"
+ " </v2:values>"
+ " </v2:item>"
+ " </v2:listOfParamNameValues>"
+ " </v2:parameterNameValues>"
+ " <v2:reportAbsolutePath>/Custom/Procurement/Custom Interface/XXTN_INT_RPT_ITEMS_EBS.xdo</v2:reportAbsolutePath>"
+ " </v2:reportRequest>"
+ " <v2:userID>%s</v2:userID>"
+ " <v2:password>%s</v2:password>"
+ " </v2:runReport>"
+ " </soapenv:Body>"
+ "</soapenv:Envelope>";
String requestPayload = String.format(payTemplate, uid, pwd);
String response = "";
String soapAction = "http://xmlns.oracle.com/xmlpserver/services/v2/ReportService";
try {
response = httpPost(reportServiceUrl, String.format(requestPayload), uid + ":" + pwd,
soapAction);
} catch (Exception e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("reportBytes>(.*)<\\/reportBytes>");
Matcher m = p.matcher(response);
while (m.find()) {
response = m.group(1);
response = new String(Base64.getDecoder().decode(response));
}
return response;
}
public static void main(String[] args){
String result = queryItemData();
System.out.println(result);
}
}