Recently, it was troublesome to copy and paste the result in the API test and mark it with 〇, so I made it a little ...
This time, I write the URL in a txt file, just call it, pack it in a list, and write it to a file. It feels like you're just moving, so I think it's a lot of waste, but please close your eyes: frowning2:
If you create an apitest folder directly under C, put a txt file in it and execute it, you should get the execution result ...
apiTest.txt
//The one who calls the postal code of Hyogo prefecture
https://zip-cloud.appspot.com/api/search?zipcode=6580083
https://zip-cloud.appspot.com/api/search?zipcode=6580026
https://zip-cloud.appspot.com/api/search?zipcode=6580025
https://zip-cloud.appspot.com/api/search?zipcode=6580082
https://zip-cloud.appspot.com/api/search?zipcode=6580024
ApiTest.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ApiTest {
public static void main(String[] args) throws InterruptedException {
//Path to the folder containing the API execution URL
final String filePath = "C:\\apitest\\";
//apiURL Files already prepared
List<String> apiUrl = new ArrayList<>();
//Stores API results.
List<String> apiResult = new ArrayList<>();
//File path for local
File file = new File(filePath);
try {
if (file.isDirectory()) {
String[] fileList = file.list();
//Read all the files in the folder for the time being so that you can do various things in the future with the file name str
for(String str:fileList) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath + str)))) {
//A one-line string-> line
String line;
while ((line = reader.readLine()) != null) {
apiUrl.add(line);
}
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
for(int i=0;i < apiUrl.size();i++) {
if(apiUrl.get(i) == null) {
//When there is no execution URL
apiResult.add("");
}else {
apiResult.add(callGet(apiUrl.get(i)));
}
}
//Export the result to a text file
try(FileWriter fileWrite = new FileWriter("C:\\apitest\\apiResult.txt");
PrintWriter pw = new PrintWriter(new BufferedWriter(fileWrite));) {
//Export to file
for(int i=0 ;i<apiResult.size();i++) {
pw.println(apiResult.get(i));
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static String callGet(String strGetUrl){
HttpURLConnection con = null;
StringBuffer result = new StringBuffer();
try {
URL url = new URL(strGetUrl);
con = (HttpURLConnection) url.openConnection();
//Basic authentication pattern
//con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("userName:pass".getBytes()));
con.setRequestMethod("GET");
con.connect();
//HTTP response code
final int status = con.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
//Successful communication
//Get the text
final InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
if(null == encoding){
encoding = "UTF-8";
}
final InputStreamReader inReader = new InputStreamReader(in, encoding);
final BufferedReader bufReader = new BufferedReader(inReader);
String line = null;
//Read text line by line
while((line = bufReader.readLine()) != null) {
result.append(line);
}
bufReader.close();
inReader.close();
in.close();
}else{
System.out.println(status);
}
}catch (Exception e1) {
e1.printStackTrace();
} finally {
if (con != null) {
//Disconnect
con.disconnect();
}
}
System.out.println("result=" + result.toString());
return result.toString();
}
}
In some cases, there is basic authentication, so I also described the pattern. If you get a 401 error, you usually have Basic authentication, so if you write the Basic authentication part, I feel like you can do something about it.
That's it: relaxed:
http://www.programing-style.com/android/android-api/android-httpurlconnection-get-text/
Recommended Posts