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

📄 httphandler.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 JAVA
字号:
/*
 * HttpHandler.java
 *
 * Created on 2005年11月1日, 下午1:41
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.j2medev.ch5.post.model;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import com.j2medev.ch5.post.share.MessageConstants;
import com.j2medev.ch5.post.share.ApplicationException;
import com.j2medev.ch5.post.share.ModelException;


/**
 *
 * @author Administrator
 */
public class HttpHandler {
    
    private String serverURL = "";
    /** Creates a new instance of HttpHandler */
    public HttpHandler(String url) {
        this.serverURL = url;
    }
    
    public void postArticle(Article article) throws ApplicationException,ModelException{
        HttpConnection httpConnection = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try{
            httpConnection = this.openConnection();
            dos = this.openConnectionOutputStream(httpConnection);
            //向输出流写入一个字节代表客户端要进行的操作
            dos.writeByte(MessageConstants.OP_POST_ARTICLE);
            //文章对象序列化
            article.serialize(dos);
            dos.close();
            dis = this.openConnectionInputStream(httpConnection);
        }catch(IOException ex){
            throw new ApplicationException(MessageConstants.ERROR_CANNOT_CONNECT);
        }finally{
            //关闭连接和流
            this.closeConnection(httpConnection, dos, dis);
        }
    }
    private HttpConnection openConnection() throws IOException {
        try {
            
            HttpConnection connection =
                    (HttpConnection) Connector.open(serverURL);
            connection.setRequestProperty("User-Agent",
                    System.getProperty("microedition.profiles"));
            connection.setRequestProperty("Content-Type",
                    "application/octet-stream");
            connection.setRequestMethod(HttpConnection.POST);
            return connection;
        } catch (IOException ioe) {
            throw ioe;
        }
    }
    
    private DataOutputStream openConnectionOutputStream(HttpConnection connection)
    throws IOException {
        try {
            return connection.openDataOutputStream();
        } catch (IOException ioe) {
            throw ioe;
        }
    }
    private DataInputStream openConnectionInputStream(HttpConnection connection)
    throws IOException,ApplicationException,ModelException {
        try {
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpConnection.HTTP_OK){
                DataInputStream inputStream =
                        connection.openDataInputStream();
                int returnCode = inputStream.readInt();
                switch (returnCode) {
                    //返回值一切正常,没有任何错误
                    case MessageConstants.ERROR_NONE: {
                        return inputStream;
                    }
                    //出现了错误,根据ModelException的causecode来判断错误
                    case MessageConstants.ERROR_MODEL_EXCEPTION:{
                        try{
                            throw ModelException.deserialize(inputStream);
                        }finally{
                            try{
                                inputStream.close();
                            }catch(IOException ex){
                                
                            }
                        }
                    }
                    //默认为连接错误
                    default: {
                        throw new ApplicationException(MessageConstants.ERROR_CANNOT_CONNECT);
                    }
                }
            }
            throw new ApplicationException(MessageConstants.ERROR_CANNOT_CONNECT);
        } catch (IOException ioe) {
            throw ioe;
        }
    }
    
    void closeConnection(HttpConnection connection,
            DataOutputStream outputStream,
            DataInputStream inputStream) {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ioe) {}
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ioe) {}
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException ioe) {}
        }
        return;
    }
}

⌨️ 快捷键说明

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