本文來自[http://blog.csdn.net/hellogv/](http://blog.csdn.net/hellogv/),引用必須注明出處!??????
?????? 最近半年都忙著Android TV項目,在春節假期才有時間寫點東西。先在這里給大家拜個年,祝大家龍年快樂...
?????? 直接進入主題:本文將會教大家如何實現一個簡單的代理服務器(僅支持Http Get),與Android的MediaPlayer結合,從而可以擴展出“播放 防盜鏈的媒體文件”,“邊播放邊保存”等的功能。
?????? 本文的代碼可以到這里下載:[http://download.csdn.net/detail/hellogv/4047134](http://download.csdn.net/detail/hellogv/4047134),代碼分為兩個工程:
1. J2SE工程:HttpGetProxy,在PC上實現簡單的代理服務器,核心類是HttpGetProxy.java,非常容易使用,這里就不嘮叨了直接貼出運行效果圖:
1. Android工程:本文重點,必須嘮叨一下。MediaPlayer播放網絡音頻([http://blog.csdn.net/hellogv/article/details/6406732](http://blog.csdn.net/hellogv/article/details/6406732))與HttpGetProxy.java結合,通過代理服務器播放網絡音頻。
?? 接下來貼出HttpGetProxy.java的原理圖:

?接下來貼出HttpGetProxy.java的源碼:
通過RemoteSocket的out_remoteSocket可以訪問防盜鏈資源,HttpGetProxy通過2個線程來實現轉發,可以在兩個線程內實現保存的功能。
~~~
package com.musicplayer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import android.util.Log;
public class HttpGetProxy {
final private String LOCAL_IP_ADDRESS = "127.0.0.1";
final private int HTTP_PORT = 80;
private ServerSocket localServer = null;
private Socket localSocket = null;
private Socket remoteSocket = null;
private String remoteIPAddress;
private InputStream in_remoteSocket;
private OutputStream out_remoteSocket;
private InputStream in_localSocket;
private OutputStream out_localSocket;
private interface OnFinishListener {
void onFinishListener();
}
public HttpGetProxy(int localport) {
// --------建立代理中轉服務器-----------//
try {
localServer = new ServerSocket(localport, 1,
InetAddress.getByName(LOCAL_IP_ADDRESS));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 結束時,清除所有資源
*/
private OnFinishListener finishListener =new OnFinishListener(){
@Override
public void onFinishListener() {
System.out.println("..........release all..........");
Log.e("---->","..........release all..........");
try {
in_localSocket.close();
out_remoteSocket.close();
in_remoteSocket.close();
out_localSocket.close();
localSocket.close();
remoteSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void startProxy(String remoteIpAddr) throws IOException {
remoteIPAddress = remoteIpAddr;
SocketAddress address = new InetSocketAddress(remoteIPAddress,HTTP_PORT);
// --------連接目標服務器---------//
remoteSocket = new Socket();
remoteSocket.connect(address);
System.out.println("..........remote Server connected..........");
Log.e("---->","..........remote Server connected..........");
in_remoteSocket = remoteSocket.getInputStream();
out_remoteSocket = remoteSocket.getOutputStream();
System.out.println("..........init remote Server I/O..........");
/**
* 接收本地request,并轉發到遠程服務器
*/
new Thread() {
public void run() {
int bytes_read;
byte[] local_request = new byte[5120];
try {
// 本地Socket
localSocket = localServer.accept();
System.out.println("..........localSocket connected..........");
Log.e("---->","..........localSocket connected..........");
in_localSocket = localSocket.getInputStream();
out_localSocket = localSocket.getOutputStream();
System.out.println("..........init local Socket I/O..........");
Log.e("---->","..........init local Socket I/O..........");
String buffer = "";
while ((bytes_read = in_localSocket.read(local_request)) != -1) {
String str = new String(local_request);
System.out.println("localSocket " + str);
Log.e("localSocket---->",str);
buffer = buffer + str;
if (buffer.contains("GET")
&& buffer.contains("\r\n\r\n")) {
//---把request中的本地ip改為遠程ip---//
buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteIPAddress);
System.out.println("已經替換IP");
out_remoteSocket.write(buffer.getBytes());
out_remoteSocket.flush();
continue;
} else{
out_remoteSocket.write(buffer.getBytes());
out_remoteSocket.flush();
}
}
System.out.println("..........local finish receive...........");
Log.e("---->","..........local finish receive..........");
finishListener.onFinishListener();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
/**
* 接收遠程服務器reply,并轉發到本地客戶端
*/
new Thread() {
public void run() {
int bytes_read;
byte[] remote_reply = new byte[5120];
try {
System.out.println("..........remote start to receive...........");
Log.e("---->","..........remote start to receive..........");
while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) {
//System.out.println("remoteSocket " + remote_reply.length);
//System.out.println("remoteSocket " + new String(remote_reply));
out_localSocket.write(remote_reply, 0, bytes_read);
out_localSocket.flush();
}
System.out.println("..........remote finish receive...........");
Log.e("---->","..........remote finish receive..........");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
~~~
?
- 前言
- Android提高第一篇之MediaPlayer
- Android提高第二篇之SurfaceView的基本使用
- Android提高第三篇之SurfaceView與多線程的混搭
- Android提高第四篇之Activity+Intent
- Android提高第五篇之Service
- Android提高第六篇之BroadcastReceiver
- Android提高第七篇之XML解析與生成
- Android提高第八篇之SQLite分頁讀取
- Android提高第九篇之SQLite分頁表格
- Android提高第十篇之AudioRecord實現"助聽器"
- Android提高第十一篇之模擬信號示波器
- Android提高第十二篇之藍牙傳感應用
- Android提高第十三篇之探秘藍牙隱藏API
- Android提高第十四篇之探秘TelephonyManager
- Android提高第十五篇之ListView自適應實現表格
- Android提高十六篇之使用NDK把彩圖轉換灰度圖
- Android上使用ASIFT實現對視角變化更魯棒的特征匹配
- 在Android上使用ZXing識別條形碼/二維碼
- Android提高十七篇之多級樹形菜單的實現
- Android-opencv之CVCamera
- Android提高十八篇之自定義Menu(TabMenu)
- Android提高第十九篇之"多方向"抽屜
- Android提高第二十篇之MediaPlayer播放網絡音頻
- Android提高第二十一篇之MediaPlayer播放網絡視頻
- android平板上的GridView視圖緩存優化
- 精確監聽AbsListView滾動至底部
- 可動態布局的Android抽屜之基礎
- 可動態布局的Android抽屜之完整篇
- Android MediaPlayer與Http Proxy結合之基礎篇