This time I will briefly summarize how to use HttpUrlConnection. HttpURLconnection is to support requests such as get, post, put, delete based on http. Usually the get and post methods are often used. Therefore, this article also explains the get and post methods.
First, specify getUrl and use getUrl as a parameter to create an instance of the URL class. Tried to make it. Once you get the connection target, start communication, return the response code, and if successful, the input stream Read the data information with.
String getUrl = "https://github.com/Hyman1993";
URL url = new URL(getUrl);
//instance of connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Specify request method
connection.setRequestMethod("GET");
//Start communication
connection.connect();
//Return response code
int responseCode = connection.getResponseCode();
//Judge the response code, if OK, proceed
if(responseCode == HttpURLConnection.HTTP_OK){
//Successful communication
//Get the text
InputStream in= connection.getInputStream();
String encoding = con.getContentEncoding();
if(null == encoding){
encoding = "UTF-8";
}
StringBuffer result = new StringBuffer();
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);
}
//Close
bufReader.close();
inReader.close();
in.close();
//Output
Log.log("result=============:"+result);
}
Also, when passing parameters, you can define the url as follows.
String getUrl = "https://github.com/Hyman1993?userName=Hyman1993&password=123456";
Regarding the POST method, the most important thing is to get the output stream from the connection and write the data information to the server. Next, let's explain with an example.
Please refer to the source below.
String postUrl = "https://github.com/Hyman1993";
URL url = new URL(postUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Designated for POST
connection.setRequestMethod("POST");
//Output possible
connection.setDoOutput(true);
//Can be entered
connection.setDoInput(true);
//No cache
connection.setUseCaches(false);
//Specify the data type as json
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//Connection, communication start
connection.connect();
//Write json data to output stream
String body = "userName=hyman1993&password=123456";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
//Successful communication
//GET method as well
}
String postUrl = "https://github.com/Hyman1993";
URL url = new URL(postUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "file/*");
connection.connect();
OutputStream outputStream = connection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream("file");
int length = -1;
byte[] bytes = new byte[1024];
while ((length = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes,0,length);
}
fileInputStream.close();
outputStream.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
//Successful communication
//GET method as well
}
Thank you for reading to the end. This time, I plan to summarize how to use SocketChannel.
Recommended Posts