⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.java

📁 基于J2ME
💻 JAVA
字号:
package com.yxw.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.yxw.interfaces.Cancelable;
import com.yxw.interfaces.HttpListener;
import com.yxw.model.PlayList;

public class Connection extends Thread implements Cancelable{

	private boolean isProxy;
	private String url;
	private boolean isCancel;
	private HttpListener listener=null;
	private int dataType;
	private boolean isFinish;
	private long startRange,endRange;//如果是读取音频数据必须设置这两个值
	private byte[] inb=null;
	private static long fileSize=-1;
	//private int cacheSize;
	private static String CMWAP_ADDR="http://10.0.0.172";
	public Connection(String url){
		isProxy=true;
		this.url=url;
		this.isCancel=false;
		this.dataType=Constant.LIEBIAO;
		this.isFinish=false;
		//this.cacheSize=200;
		
	}
	public Connection(boolean isProxy,String url,boolean isCancel,int dataType,long startRange,long endRange,byte[] inb,HttpListener listener){
		this.isProxy=isProxy;
		this.url=url;
		this.isCancel=isCancel;
		this.listener=listener;
		this.dataType=dataType;
		this.startRange=startRange;
		this.endRange=endRange;
		this.inb=inb;
	}
	public void cancel() {
		// TODO 自动生成方法存根
		isCancel=true;
	}
	private InputStream getConnectInputStream(long startRange,long endRange){
		HttpConnection	hc=null;
		InputStream input=null;
		String HttpStringIP = "";//主机IP
		String HttpStringAdd = "";//资源地址
		String HttpPort = "";//端口
		System.out.println("url:"+url);
		if(isProxy){
			
			int runNums = 0;
			int getNumsString = 0;
			for (int i = 7;i < url.length() ; i++){
				if (url.charAt(i) == '/'){
					runNums = i;
					i = url.length();
				}
			}
			if (runNums > 0){
				HttpStringIP = url.substring(7,runNums);
				
				int portnums = 0;
				for(int i = 0;i < HttpStringIP.length();i++){
					if(HttpStringIP.charAt(i) == ':'){
						portnums = i;
						i = HttpStringIP.length();
					}//end if

				}//end for i
				
				if(portnums != 0){
					HttpPort = HttpStringIP.substring(portnums+1,HttpStringIP.length());
					HttpStringIP = HttpStringIP.substring(0,portnums);
				}//end if
				else{
					HttpPort = "80";
				}
				//System.out.println("HttpPort = " + HttpPort);
				//System.out.println("HttpStringIP = " + HttpStringIP);
				//System.out.println("url = " + url);
				HttpStringAdd = url.substring(runNums,url.length());
				System.out.println("HttpStringAdd = " + HttpStringAdd);
			}
				try {
					hc = (HttpConnection)javax.microedition.io.Connector.open("http://10.0.0.172"+HttpStringAdd,Connector.READ,true);
					
					hc.setRequestMethod(HttpConnection.GET);
					hc.setRequestProperty("X-Online-Host", HttpStringIP+":"+HttpPort);
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
					if(this.listener!=null){
						this.listener.onError(-1, e.getMessage());
					}
					
				}
				
				//Host:  wap.3g.cn
				//hc.setRequestMethod("")
		}else{
			   try {
				hc = (HttpConnection)javax.microedition.io.Connector.open(url);
				hc.setRequestMethod(HttpConnection.GET);
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}		 
			   
		}						
				//hc.setRequestProperty("Host",HttpStringIP);
				try {
					hc.setRequestProperty("IF-Modified-Since", "15 Oct 2003 08:47:14 GMT");
					hc.setRequestProperty("User-Agent",
					"Nokia6600/1.0 (4.04.0) SymbianOS/7.0s Series60/2.0 Profile/MIDP-2.0 Configuration/CLDC-1.0");
					hc.setRequestProperty("Content-Language", "en-CA");
					hc.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
//					hc.setRequestProperty("Connection", "Keep-Alive");
					hc.setRequestProperty("Connection", "close");
					
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
					if(this.listener!=null){
						this.listener.onError(-1, e.getMessage());
					}
				}
				
				
				
				if(this.dataType==Constant.YINPIN){
					try {
						hc.setRequestProperty("Range", "bytes="+startRange+"-"+endRange);
						System.out.println(startRange+"--"+endRange);
					} catch (IOException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
						if(this.listener!=null){
							this.listener.onError(-1, e.getMessage());
						}
					}
				}
		//读取响应数据
				int code,size;
				try {
					code = hc.getResponseCode();
					  if(code!=HttpConnection.HTTP_OK&&code!=HttpConnection.HTTP_PARTIAL) {
				        	if(listener!=null){
				              listener.onError(code, hc.getResponseMessage());
				        	}
				            return null;
				        }
				        // get size:
				         size = (int)hc.getLength(); // 返回响应大小,或者-1如果大小无法确定
				         if(this.inb==null&&this.dataType==Constant.LIEBIAO){
				        	 this.inb=new byte[size];
				         }
				        if(listener!=null){
				        	listener.onSetSize(size,fileSize);
				        }
				        // 开始读响应:
				         input = hc.openInputStream();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					if(listener!=null){
						//listener.onError(code, message)
					}
					e.printStackTrace();
					if(this.listener!=null){
						this.listener.onError(-1, e.getMessage());
					}
				}
		      //返回输入流
		return input;
	} 
	public int readYinpin(InputStream in){
		if(in==null){
			return -1;
		}
		long start=System.currentTimeMillis();
		int length=0;
		int haveDown=0;
		//length=in.read(inb);
		//System.out.println("length:"+length);
		
		try {
			while((length=in.read(inb,haveDown,inb.length-haveDown))>0&&haveDown<inb.length){
				//System.out.println("length:"+length);
				haveDown+=length;
				this.listener.onProgress(haveDown);
			}
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			this.isFinish=true;
			e.printStackTrace();
			return -1;
		}
		long end =System.currentTimeMillis();
		System.out.println("time(ms):"+(end-start));
		this.listener.onFinish(null, haveDown);//将已下载的数据量回传
		this.isFinish=true;
		return haveDown;
	}
	public void readLiebiao(InputStream in){
		if(in==null){
			return;
		}
		long start=System.currentTimeMillis();
		int length=0;
		int haveDown=0;
		//length=in.read(inb);
		//System.out.println("length:"+length);
		ByteArrayInputStream inputStream;
		
		try {
			while((length=in.read(inb,haveDown,inb.length-haveDown))>0&&haveDown<inb.length){
				//System.out.println("length:"+length);
				haveDown+=length;
				this.listener.onProgress(haveDown);
			}
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			this.isFinish=true;
			e.printStackTrace();
			
		}
		long end =System.currentTimeMillis();
		System.out.println("time(ms):"+(end-start));
		this.listener.onFinish(this.inb, haveDown);//将已下载的数据量回传
		this.isFinish=true;
		
	}
	public static long getFileSize(String url){
		HttpConnection hc=null;
		 try {
				hc = (HttpConnection)javax.microedition.io.Connector.open(url);
				hc.setRequestMethod(HttpConnection.GET);
				int code;
				long size;
					code = hc.getResponseCode();
					  if(code!=HttpConnection.HTTP_OK) {
						  System.out.println("not exist:"+code);
				        	return -1;
				        }
				        // get size:
				         size = (int)hc.getLength(); // 返回响应大小,或者-1如果大小无法确定
				         hc.close();
				         fileSize=size;
				       return size;
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}		 
			
		return -1;
	}
	//public 
	public void run(){
		while(!this.isCancel&&!this.isFinish){
			if(this.dataType==Constant.YINPIN){
			InputStream in=this.getConnectInputStream(startRange, endRange);
            this.readYinpin(in);
			}else if(this.dataType==Constant.LIEBIAO){
				InputStream in=this.getConnectInputStream(0, 0);
	            this.readLiebiao(in);
			}
		}
	}
	public int getDataType() {
		return dataType;
	}
	public void setDataType(int dataType) {
		this.dataType = dataType;
	}
	public boolean isProxy() {
		return isProxy;
	}
	public void setProxy(boolean isProxy) {
		this.isProxy = isProxy;
	}
	public HttpListener getListener() {
		return listener;
	}
	public void setListener(HttpListener listener) {
		this.listener = listener;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public byte[] getInb() {
		return inb;
	}
	public void setInb(byte[] inb) {
		this.inb = inb;
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -