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

📄 wshttpdemo.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.* ;
import java.io.*;
public class WShttpDemo extends MIDlet
	implements CommandListener
{
    Form mainForm = new Form ("WShttpDemo");
	TextField symbolField = 
		new TextField ("Symbol", "ibm", 5, TextField.ANY);
	StringItem resultItem = 
		new StringItem ("", "");
	Command getCmd = 
		new Command ("Get", Command.SCREEN, 1);
	Command exitCmd = 
		new Command ("Exit", Command.EXIT, 0);

	public WShttpDemo () {
		mainForm.append (symbolField);
		mainForm.append (resultItem);
		mainForm.addCommand (getCmd);
		mainForm.addCommand (exitCmd);
		mainForm.setCommandListener (this);
	}

	public void startApp()
	{
		Display.getDisplay (this).setCurrent (mainForm);
		try{

			getQuote( symbolField.getString() );
		}catch(Exception e)
		{
			System.out.println( e );
		}
	}


	void getQuote(String symbol) throws IOException 
	{
		String url = "http://localhost/PersonWebService/Service1.asmx" ;

		HttpConnection c = null;
		InputStream is = null;
		OutputStream os = null;
		int rc;

		try 
		{
			c = (HttpConnection)Connector.open(url);

			c.setRequestMethod(HttpConnection.POST);
			
			c.setRequestProperty("Content-Type","text/xml; charset=utf-8");
			c.setRequestProperty("SOAPAction","mysoapaction:/getQuote");


			String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"urn:MyCompanyInfo/Service1\" xmlns:types=\"urn:MyCompanyInfo/Service1/encodedTypes\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
  "<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
    "<getQuote>" +
      "<symbol xsi:type=\"xsd:string\">" + symbol + "</symbol>" + 
    "</getQuote>" +
  "</soap:Body>" +
"</soap:Envelope>";


			os = c.openOutputStream();

			OutputStreamWriter osw = new OutputStreamWriter( os, "utf-8" );
			osw.write( s, 0, s.length() );
			osw.flush();
			osw.close();

			rc = c.getResponseCode();
			if (rc != HttpConnection.HTTP_OK) 
			{
				throw new IOException("HTTP response code: " + rc);
			}
			System.out.println( rc );

			is = c.openInputStream();

			String type = c.getType();
			System.out.println( type );

			int len = (int)c.getLength();
			System.out.println( len );
			if (len > 0) 
			{
				int actual = 0;
				int bytesread = 0 ;
				byte[] data = new byte[len];
				while ((bytesread != len) && (actual != -1)) 
				{
					actual = is.read(data, bytesread, len - bytesread);
					bytesread += actual;
				}
				process(data);
			} 
			else 
			{
				int ch;
				while ((ch = is.read()) != -1) 
				{
					process((byte)ch);
				}
			}
		} 
		catch (ClassCastException e) 
		{
			throw new IllegalArgumentException("Not an HTTP URL");
		} 
		finally 
		{
			if (is != null)
				is.close();
		}
	}

	void process(byte[] data)
	{
		String str = "";
		try{
			str = new String(data, "utf-8");
		}
		catch(java.io.UnsupportedEncodingException e )
		{
			System.out.println( e );
		}

		System.out.println( str );
		resultItem.setText( str );
	}

	void process(byte ch)
	{
		resultItem.setText( resultItem.getText() + (char) ch );
	}

	public void pauseApp()
	{}
	public void destroyApp(boolean unconditional)
	{}

	public void commandAction(javax.microedition.lcdui.Command c,
		javax.microedition.lcdui.Displayable d) 
	{
		try
		{
			if(c.equals(getCmd))
			{
				Object cSR;

				//cSR = 
					getQuote( symbolField.getString() );
				//System.out.println( cSR.toString() );
				//resultItem.setText( cSR.toString() );

				/*
				SoapObject objResult = (SoapObject) cSR; 
				
					if ( objResult != null )
					{
						resultItem.setText("The result is " +
							objResult.getProperty(0));
					}//end if ( objResult != null )
					else
					{
						resultItem.setText("Nothing");
					}
				*/
			}

			else if(c.equals(exitCmd))
			{
				destroyApp(false);
				notifyDestroyed();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			resultItem.setLabel("Error: " + e);
			System.out.println(e);
		}
	}

}

⌨️ 快捷键说明

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