|
On its own Android learning to be a summary, for easy viewing later.
First, the Android transmitting Get way http requests using a Java standard class, but also relatively simple.
Mainly divided into the following steps:
1. Construction URL
URL url = new URL (String path);
2. Set up a connection
httpURLConnection = (HttpURLConnection) url.openConnection ();
//overtime time
httpURLConnection.setConnectTimeout (3000);
// Indicate this http request settings using GET method
httpURLConnection.setRequestMethod ( "GET");
int responsecode = httpURLConnection.getResponseCode (); // return to the response to the number, such as: HTTP_OK indicates a successful connection.
3. Get Back Data
if (responsecode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream ();
} // Get inputStream easier to handle it.
new InputStreamReader (inputStream, "utf-8")
4. Close the connection
void disconnect ()
Second, by following a simple way to get the realization Demo Request:
package com.http.get;
import java.io.BufferedReader;
import java.io.FileOutputStream;
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.URL;
public class HttpUtils {
private static String URL_PATH = "http://www.baidu.com";
private static HttpURLConnection httpURLConnection = null;
public HttpUtils () {
}
public static void shuchu () {
InputStream inputStream = getInputStream ();
String result;
try {
BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream, "utf-8"));
result = "";
String line = "";
try {
while ((line = reader.readLine ())! = null) {
result = result + line;
}
} Catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
System.out.println (result);
httpURLConnection.disconnect ();
} Catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
/ **
* Get the server data to return InputStream
* @return
* /
public static InputStream getInputStream () {
InputStream inputStream = null;
try {
URL url = new URL (URL_PATH);
if (url! = null) {
try {
httpURLConnection = (HttpURLConnection) url.openConnection ();
//overtime time
httpURLConnection.setConnectTimeout (3000);
// Indicate this http request settings using GET method
httpURLConnection.setRequestMethod ( "GET");
int responsecode = httpURLConnection.getResponseCode ();
if (responsecode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream ();
}
} Catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
} Catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
return inputStream;
}
public static void main (String [] args) {
// Save the file locally
// SaveImageToDisk ();
shuchu ();
}
}
// Because get this way is ava standard class, direct write java program, but all the same, android is also the same. .
Simple visited Baidu, Baidu search page is returned source: Image not been uploaded. . . No screenshot friends.
The right to return is that you right-click on the page to have a look at the source code, it is returned, it is output, they can go to the next comparison is not the same, is the same. |
|
|
|