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

📄 connectionmanager.java

📁 j2me实例代码
💻 JAVA
字号:
package ConnectionImpl;

import java.lang.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;

public class ConnectionManager {
	private HttpConnection con;
	private InputStream is;
	private OutputStream os;
	private final String baseurl="http://127.0.0.1:8888/jsp-examples/jsp2/el/basic-arithmetic.jsp";
	private String ua;
	private String locale;
	private String msg;
	private String user;
	private String password;
	ConnectionManager(){
		StringBuffer sb=new StringBuffer(60);
		sb.append("Configuration/");
		sb.append(System.getProperty("microedition.configuration"));
		String prof=System.getProperty("microedition.profiles");
		int i=0,j=0;
		while((j=prof.indexOf(' ',i))!=-1){
			sb.append(" Profile/");
			sb.append(prof.substring(i,j));
			i=j+1;
		}
		sb.append(" Profile/");
		sb.append(prof.substring(i));
		ua=sb.toString();
		
		locale=System.getProperty("microedition.locale");
		if(locale==null){
			locale="en-US";
		}
				
	}
	
	byte[] Process(){
		byte[] data=null;
		try{
			open();
			int n=(int)con.getLength();
			if(n>0){
				int bytesread=0;
				data=new byte[n];
				for(int offset=0;offset<n;offset+=bytesread){
					bytesread=is.read(data,offset,n-bytesread);
				}
			}
		}catch(IOException ioe){			
		}finally{
			try{
				if(con!=null){
					con.close();
				}if(os!=null){
					os.close();
				}if(is!=null){
					is.close();
				}
			}catch(IOException ioe){			
			}		
		}
		return data;
	}
	
	private void open() throws IOException{
		int status=-1;
		String url=baseurl;
		String auth=null;
		is=null;
		os=null;
		con=null;
		
		System.out.println("aaaaaaaaaaa");
		while(con==null){
			System.out.println("bbbbbbbbb");
			con=(HttpConnection)Connector.open(url);
			con.setRequestMethod(HttpConnection.POST);
			con.setRequestProperty("User-Agent",ua);
			con.setRequestProperty("Accept-Language",locale);
			con.setRequestProperty("Content-Type","text/plain");
			con.setRequestProperty("Accept","text/plain");
			if(user!=null && password!=null){
				con.setRequestProperty("Authorization","Basic "+BasicAuth.encode(user,password));
			}
			os=con.openOutputStream();
			os.write(msg.getBytes());
			os.close();
			os=null;
			
			status=con.getResponseCode();
			System.out.println("staus"+status);
			switch(status){
				case HttpConnection.HTTP_OK:
				System.out.println("ok");
				break;
				case HttpConnection.HTTP_TEMP_REDIRECT:
				case HttpConnection.HTTP_MOVED_TEMP:
				case HttpConnection.HTTP_MOVED_PERM:
					System.out.println("ok");
					url=con.getHeaderField("location");
					con.close();
					con=null;
					break;
					default:
						con.close();
					throw new IOException("Response status not OK:"+status);
			}
		}
		System.out.println("ccccccccccc");
		
		is=con.openInputStream();
	}
	void setMsg(String s){
		msg=s;
	}
	void setUser(String s){
		user=s;
	}
	void setPassword(String s){
		password=s;
	}
}

⌨️ 快捷键说明

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