2011-05-04 66 views
0

我累到我的应用程序与远程服务器连接,并通过一些证书给它正确的方式,但我始终从服务器获取一个同样的反应。我尝试更改所有参数值和其他请求标头值,但仍然无法达到确切的解决方案。我需要你,无论我是否使用正确的方式来ping服务器并传递值。什么是实现HttpConnection在黑莓

下面是我使用,请让我知道,如果我有错了地方的代码。

// HttpServiceConnection.java

import java.io.InputStream; 
import java.io.OutputStream; 
import java.nio.ByteBuffer; 

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

import net.rim.device.api.system.DeviceInfo; 

import com.beacon.bb.app.util.WSMConfig; 

/** 
* @author N******** 
* 
*/ 
public class HttpServiceCommunication { 

public HttpServiceCommunication() { 
    System.out.println("Http Service Communication Called"); 
} 

public String sendHttpPost(String uri, String email, String uid, 
     String pass) throws Exception { // Hashtable header 
    String response = null; 
    // create the connection... 
    System.out.println("Url    " + uri); 
    HttpConnection _connection = null; 
    String params = null; 
    if (DeviceInfo.isSimulator()) { 
     params = ";deviceside=false"; 
    } else { 
     params = ";deviceside=true;interface=wifi"; 
    } 

    String URL = uri + params; 
    System.out.println("Connecting to Http Connection "); 
    try { 
    _connection = (HttpConnection) Connector.open(URL); 
    } catch(Exception e){ 
     e.printStackTrace(); 
    } 

    if (_connection != null) { 

     _connection.setRequestMethod(HttpConnection.POST); 
     System.out.println("After Request Method "); 
     _connection.setRequestProperty("User-Agent", 
       "Profile/MIDP-2.0 Configuration/CLDC-1.1"); 
     _connection.setRequestProperty("Content-Language", "en-US"); 
     _connection.setRequestProperty("Content-type", "application/json"); 

     // setting header if any 
     // if (header != null) { 
     // for (Enumeration en = header.keys(); en.hasMoreElements();) { 
     // String key = (String) en.nextElement(); 
     // String value = (String) header.get(key); 
     // _connection.setRequestProperty(key, value); 

     _connection.setRequestProperty("email", email); 
     //_connection.setRequestProperty("method","login"); 
     _connection.setRequestProperty("uid", uid); 
     _connection.setRequestProperty("password", pass); 

     //_connection.setRequestProperty("uid", uid); 

     // } 
     // } 
     System.out.println("Open Output Stream  "); 
     // System.out.println("Data is     "+data); 
     OutputStream _outputStream = _connection.openOutputStream(); 
     //System.out.println("Writing data  "); 
     //_outputStream.write(data); 
     // _outputStream.flush(); // Optional, getResponseCode will flush 

     // Getting the response code will open the connection, send the 
     // request, and read the HTTP response headers. 
     // The headers are stored until requested. 
     try { 
     System.out.println("Response Code :" + _connection.getResponseCode()); 
     int rc = _connection.getResponseCode(); 
     System.out.println("Response Code :" + rc); 
     System.out.println("Response Code   :" + rc + " if HTTP OK    :" 
       + (rc == HttpConnection.HTTP_OK)); 
     if (rc == HttpConnection.HTTP_FORBIDDEN) { 
      System.out.println("FORBIDDEN"); 
      response = WSMConfig.NOT_AUTH; 
     } else if (rc != HttpConnection.HTTP_OK) { 
      response = WSMConfig.NOT_OK; 
     } else if (rc == HttpConnection.HTTP_OK) { 
      InputStream _inputStream = _connection.openInputStream(); 
      final int MAX_LENGTH = 128; 
      byte[] buf = new byte[MAX_LENGTH]; 
      int total = 0; 
      while (total < MAX_LENGTH) { 
       int count = _inputStream.read(buf, total, MAX_LENGTH 
         - total); 
       if (count < 0) { 
        break; 
       } 
       total += count; 
      } 
      response = new String(buf, 0, total); 
      //ByteBuffer bb = new ByteBuffer(_inputStream); 
      //response = bb.getString(); 
      System.out.println("Response from Server   :" + response); 
      // close everything out 
      { 
       if (_inputStream != null) 
        try { 
         _inputStream.close(); 
        } catch (Exception e) { 
        } 
       if (_outputStream != null) 
        try { 
         _outputStream.close(); 
        } catch (Exception e) { 
        } 
       if (_connection != null) 
        try { 
         _connection.close(); 
        } catch (Exception e) { 
        } 
      } 
     } 
    else { 
     response = WSMConfig.SERVER_ERROR; 
    } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

} 
    //System.out.println("Response :" + response); 
    return response; 

} 

} 

我越来越喜欢{"code":0,"err":"Missing 'method'."}

任何帮助的回应是值得重视的....

感谢

+0

其余它看起来像你的代码是罚款(这是从服务器的有效JSON响应)。您是否尝试过在取消'// _ connection.setRequestProperty(“法”,“登录”);' – jprofitt 2011-05-04 19:34:18

+0

是的,我试图用这种方式过,甚至我很清楚地意识到,必须要传递给服务器的值。我也使用webclient进行了测试,但仍然得到了相同的响应。请让我知道,如果有人找到解决方案。 – 2011-05-05 06:45:50

回答

1

尝试了这一点,当你想将数据传递到服务器:

//encode your data to send 
URLEncodedPostData encoder = new URLEncodedPostData(null, false); 
encoder.encode("email", email); 
encoder.encode("method", "login"); 
encoder.encode("uid", uid); 
encoder.encode("password", pass); 

//Now you open up an output stream to write to the connection 
OutputStream os = _connection.openOutputStream(); 
os.write(encoder.getBytes(); 
os.flush(); 

,然后继续你的逻辑