一、目的
愉快的結(jié)束Java的學(xué)習(xí),然后步入安卓開發(fā)。大致了解整個網(wǎng)絡(luò)的運(yùn)營:web前端、移動端、服務(wù)器端。
二、知識點(diǎn)
1.開發(fā)介紹
默認(rèn)端口:80
- http://www.baidu.com
- http://www.baidu.com/download?type=jpg&categ
- c/s:customer / sever
- 移動端:app
1 Java、Kotlin——Android
2 OC、Swift——IOS
- 瀏覽器:IE 火狐
1 HTML語言(H5-Html5):超文本的標(biāo)記語言
2 CSS讓網(wǎng)友更美觀
3 JavaScript(js):腳本語言
- 服務(wù)器端:提供數(shù)據(jù)存儲和訪問的接口 API
API=Application Programming Interface
- 如果需要將自己本地的數(shù)據(jù)提供給外部訪問
1 自己的電腦扮演的就是服務(wù)器端
2 需要自己配置一個服務(wù)器 apache
3 Windows: 必須自己搭建服務(wù)器:apache tomcat
4 mac:sudo apachectl start/stop
2.HTML的簡單使用
- form提交登錄表單
- 使用服務(wù)器訪問局域網(wǎng)數(shù)據(jù)
將自己電腦的IP找出 組成:http:// (+ip) +.html只要訪問者和自己是再同一個網(wǎng)絡(luò)下,訪問者就能打開該網(wǎng)頁 - 使用get方式提交數(shù)據(jù)
注:詳細(xì)可以查看菜鳥教程
打開結(jié)果,如:
3.Java部分
- socket提交數(shù)據(jù)給服務(wù)器-失敗
public static void main(String[] args) throws IOException {
使用socket簡單的只寫客戶端,是不行的
Socket socket = new Socket("10.129.2.205",800);
//傳遞的數(shù)據(jù)
String data ="user_name=Jack&user_password=123";
//創(chuàng)建輸出流
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println(data);
//接收服務(wù)器端返回的結(jié)果
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(bufferedReader.readLine());
- URL實(shí)現(xiàn)下載
//下載圖片(數(shù)據(jù)) get不帶參數(shù)
public static void getImage() throws IOException {
//URL
URL url = new URL("http://10.129.2.205/1.jpeg");
//獲取于服務(wù)器連接的對象
URLConnection connection = url.openConnection();
//獲取輸入流 讀取下注的內(nèi)容
InputStream is = connection.getInputStream();
//創(chuàng)建文件保存的位置
FileOutputStream fos = new FileOutputStream("F:\\Androidstudio\\java2\\src\\main\\java\\day14\\1.jpeg");
byte[] buf = new byte[1024];
int len;
while((len = is.read(buf)) != -1){
fos.write(buf,0,len);
}
}
//帶參數(shù)的get請求
public static void getParame() throws IOException {
java.lang.String path = "http://10.129.2.205/qqq.php? " + "user_name=Jack&user_password=123&提交=提交";//因為這一步,所以不需要單獨(dú)發(fā)送數(shù)據(jù)了
URL url = new URL(path);
//獲取連接的對象 URLConnection封裝了Socket
URLConnection connection = url.openConnection();
//設(shè)置請求方式
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestMethod("GET");
//接收數(shù)據(jù)
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// System.out.println(bufferedReader.readLine());
InputStream is = httpURLConnection.getInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
System.out.println(new java.lang.String(buf, 0, len));
}
}
}
- 使用post上傳
//使用post上傳簡單數(shù)據(jù)(不是文件)
public static void post() throws IOException {
URL url = new URL("http://10.129.2.205/qqq.php? ");
//2.獲取connection對象
//URLConnection
//HttpURLConnection 自己需要設(shè)定請求的內(nèi)容
// 請求方式 上傳的內(nèi)容
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//3.設(shè)置請求方式為post
connection.setRequestMethod("POST");
//設(shè)置由輸出流(需要上傳)
connection.setDoOutput(true);
//設(shè)置由輸入流 (需要下載)
connection.setDoInput(true);
//4.準(zhǔn)備上傳的數(shù)據(jù)
String data = "user_name=jack&user_password=123";
//上傳文件
//FileInputStream
//5.開始上傳 輸出流對象
OutputStream os = connection.getOutputStream();
os.write(data.getBytes());
os.flush();//寫完了
//6.接收服務(wù)端返回的數(shù)據(jù)
InputStream is = connection.getInputStream();
byte[] buf = new byte[1024];
int len ;
while ((len = is.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
}
三、心得
今天真的好累好累,感覺走路都要睡著了一樣。Java學(xué)到這里就算結(jié)束了,其中的知識點(diǎn)大都學(xué)到了,不過還不熟悉,自己要多多練習(xí)才能從菜鳥晉級?。。?!
愛因斯坦說過:“我們一來到世間,社會就在我們面前樹起一個巨大的問號:你怎樣度過自己的一生?”這是一個值得思考的問題,既然現(xiàn)在我們進(jìn)入編程的領(lǐng)域,那就認(rèn)證去學(xué),決定自己未來的人生。