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

📄 kbhttptransport.java

📁 In the last three articles, I’ve been walking you through the creation of an end-to-end BlackBerry a
💻 JAVA
字号:
package KnowledgeBase;

import java.io.*;
import java.lang.*;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;

public final class KbHTTPTransport implements HTTPTransport {

    public static String REQ_METHOD_POST = "POST";
    public static String REQ_METHOD_GET = "GET";

    private HTTPTransportListener listener = null;
    public static final int NOT_IN_COVERAGE = Integer.MAX_VALUE;

    public synchronized void send(final String requestMethod, final String URL, final String requestData){
        send(requestMethod, URL, requestData, null, null);
    } 
     
    public synchronized void send(final String requestMethod, final String URL, final String requestData, 
                                       final String[] headerKeys, final String[] headerValues){
        final String data = requestData;
        // we want to thread this out so it doesn't block the calling class
        // connections should be made in the background
        Thread t = new Thread(new Runnable(){
            public void run(){
                try {
                    // setup the connection
                    HttpConnection http; 
                    
                    if (!TransportUtils.isInCoverage()){
                        processResponse(NOT_IN_COVERAGE, "");
                        return;
                    }
                    
                    if (TransportUtils.isDevicesideRequired()) {
                        http = (HttpConnection)Connector.open(URL + ";deviceside=false");
                    } else {
                        http = (HttpConnection)Connector.open(URL);
                    }
                    
                    http.setRequestMethod(requestMethod);
                    // send the data if there is some to send.
                    if (data != null){
                        DataOutputStream out = null;
                        int length = data.length();
                        String slength = String.valueOf(length);
                        http.setRequestProperty("Content-Length", slength);
                        
                        if (headerKeys != null && headerValues != null && headerKeys.length == headerValues.length){
                            for (int i = headerValues.length - 1; i >= 0; --i){
                                http.setRequestProperty(headerKeys[i], headerValues[i]);
                            } 
                        }
                        
                        try {
                            out = http.openDataOutputStream();
                            out.write(data.getBytes());
                        }
                        catch (IOException ioe) {
                        }
                        out.close();  // close the output connection.
                    }
                    // get the response (input)
                    int received;
                    InputStream in = http.openInputStream();
                    //read in all the message characters received from the server
                    StringBuffer response = new StringBuffer();
                    while((received = in.read()) != -1){
                        response.append((char) received);
                    }
                    in.close();  // close the input stream
                    int httpStatus = http.getResponseCode();
                    //close the connections
                    http.close(); // close the HTTP connection
                    processResponse(httpStatus, response.toString().trim());
                }
                catch(IOException ioe) {
                    processResponse(0, ioe.toString());
                }
            }
        });
        t.start();
    }
    
    public void setHTTPTransportListener(HTTPTransportListener t){
        listener = t;
    }
    
    public void removeHTTPTransportListener(){
        listener = null;
    }
    
    private void processResponse(int HTTPResponseCode, String data){
        if (listener != null){
            listener.processResponse(HTTPResponseCode, data);
        }
    }
}

⌨️ 快捷键说明

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