2012-08-06 100 views
0

我正在開發一個應用程序,其中我需要通過POST方法傳遞參數,所以我試圖與HTTPConnection實現,但它讓我錯誤。在沒有建立連接。在這個代碼中,我通過參數與URLEncodedPostData,但它不能問題與黑莓Http連接

這裏是我的代碼::

URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false); 
        //passing q’s value and ie’s value 
        postData.append("q", "remoQte"); 
        postData.append("ie", "UTF-8"); 

        ConnectionFactory conFactory = new ConnectionFactory(); 
        ConnectionDescriptor conDesc = null; 
        try{ 
         conDesc = conFactory.getConnection("http://www.google.co.in/search"); 
        }catch(Exception e){ 
         System.out.println(e.toString()+":"+e.getMessage()); 
        } 
        String response = ""; // this variable used for the server response 
        // if we can get the connection descriptor from ConnectionFactory 
        if(null != conDesc){ 
         try{ 
          HttpConnection connection = (HttpConnection)conDesc.getConnection(); 
          //set the header property 
          connection.setRequestMethod(HttpConnection.POST); 
          connection.setRequestProperty("Content-Length", Integer.toString(postData.size())); //body content of post data 
          connection.setRequestProperty("Connection", "close"); // close the connection after success sending request and receiving response from the server 
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // we set the content of this request as application/x-www-form-urlencoded, because the post data is encoded as form-urlencoded(if you print the post data string, it will be like this -> q=remoQte&ie=UTF-8). 

          //now it is time to write the post data into OutputStream 
          OutputStream out = connection.openOutputStream(); 
          out.write(postData.getBytes()); 
          out.flush(); 
          out.close(); 

          int responseCode = connection.getResponseCode(); //when this code is called, the post data request will be send to server, and after that we can read the response from the server if the response code is 200 (HTTP OK). 
          if(responseCode == HttpConnection.HTTP_OK){ 
           //read the response from the server, if the response is ascii character, you can use this following code, otherwise, you must use array of byte instead of String 
           InputStream in = connection.openInputStream(); 
           StringBuffer buf = new StringBuffer(); 
           int read = -1; 
           while((read = in.read())!= -1) 
            buf.append((char)read); 
           response = buf.toString(); 
          } 
          Dialog.alert("response"+ response); 

          //don’t forget to close the connection 
          connection.close(); 

         }catch(Exception e){ 
          System.out.println(e.toString()+":"+e.getMessage()); 
         } 
        } 

更新::

import java.io.IOException; 

import javax.microedition.io.HttpConnection; 

import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.container.MainScreen; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransport; 
import org.xmlpull.v1.XmlPullParserException; 

public class UiMainscreen extends MainScreen { 
    ButtonField loginButton; 
    private static final String TAG_CONTACTS = "contacts"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_EMAIL = "email"; 
    private static final String TAG_ADDRESS = "address"; 
    private static final String TAG_GENDER = "gender"; 
    private static final String TAG_PHONE = "phone"; 
    private static final String TAG_PHONE_MOBILE = "mobile"; 
    private static final String TAG_PHONE_HOME = "home"; 
    private static final String TAG_PHONE_OFFICE = "office"; 
    public UiMainscreen() { 


     final ButtonField b=new ButtonField("JSON"); 
     add(b); 

     FieldChangeListener listener=new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       if(field==b){ 

        try{  
        String URL = "http://www.google.co.in/"; 
        String METHOD_NAME = "search"; 
        String NAMESPACE = "http://tempuri.org/"; 
        String SOAP_ACTION = NAMESPACE+METHOD_NAME; 

        SoapObject resultRequestSOAP = null; 
        HttpConnection httpConn = null; 
        HttpTransport httpt; 
        SoapPrimitive response = null; 
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        request.addProperty("q", "remoQte"); 
        request.addProperty("ie", "UTF-8"); 
        System.out.println("The request is=======" + request.toString()); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.dotNet = true; 
        envelope.setOutputSoapObject(request); 

        httpt = new HttpTransport(URL); 
        httpt.debug = true; 
        try 
        { 
         httpt.call(SOAP_ACTION, envelope); 
         response = (SoapPrimitive) envelope.getResponse(); 
         String result = response.toString(); 
         resultRequestSOAP = (SoapObject) envelope.bodyIn; 
} 
        catch (IOException e) { 
         // TODO Auto-generated catch block 
         System.out.println("The exception is IO==" + e.getMessage()); 
        } catch (XmlPullParserException e) { 

         // TODO Auto-generated catch block 
         System.out.println("The exception xml parser example===" 
           + e.getMessage()); 
        } 
        System.out.println(resultRequestSOAP); 
        Dialog.alert("resultRequestSOAP"+resultRequestSOAP); 
        }catch (Exception e) { 
         System.out.println("Problem===" 
            + e.getMessage()); 
        } 
      }} 
     }; 
     b.setChangeListener(listener); 



    } 
+1

粘貼你的堆棧跟蹤! – HashimR 2012-08-06 06:53:18

+0

它讓我空指針 – 2012-08-06 06:56:38

+1

哪一行?粘貼你的stacktrace請 – HashimR 2012-08-06 07:06:10

回答

1

既然你想要將一些參數發佈到您的連接中,以下可能會有所幫助。

String URL = "http://xxx.xxx.com/xxx/xxx.asmx"; 
String METHOD_NAME = yourMethodName; 
String NAMESPACE = "http://tempuri.org/"; 
String SOAP_ACTION = NAMESPACE+METHOD_NAME; 
SoapObject resultRequestSOAP = null; 
HttpConnection httpConn = null; 
HttpTransport httpt; 
SoapPrimitive response = null; 
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
request.addProperty("username", user_id); 
request.addProperty("password", password); 
System.out.println("The request is=======" + request.toString()); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 
httpt = new HttpTransport(URL+C0NNECTION_EXTENSION); 
httpt.debug = true; 
try 
{ 
    httpt.call(SOAP_ACTION, envelope); 
    response = (SoapPrimitive) envelope.getResponse(); 
    String result = response.toString(); 
    resultRequestSOAP = (SoapObject) envelope.bodyIn; 
} 
catch (IOException e) 
{ 
    System.out.println("The exception is IO==" + e.getMessage()); 
} 
catch (XmlPullParserException e) 
{ 
    System.out.println("The exception xml parser example===" 
    + e.getMessage()); 
} 
System.out.println(resultRequestSOAP); 
return response + ""; 

這是傳遞兩個參數到web服務;用戶名和密碼。你可以相應地修改。連接擴展是指定連接類型的部分; BES,WIFI等。模擬器的內容可以添加爲connectionString = ";deviceside=true"。這可以使用Ksoap2庫,您可以通過屬性 - > Java Build Path - >庫將其添加到項目中。

+0

我已經添加了庫,但是黑莓中的'HttpTransport'類不是派生的 – 2012-08-06 07:21:51

+0

您可以使用Ksoap庫的HttpTransport。將項目庫(jar文件)包含在項目中時,可以導入org.ksoap2.transport.HttpTransport; – Sarah 2012-08-06 07:31:15

+0

你可以給我鏈接弗羅姆在哪裏我可以下載HttpTransport jar文件 – 2012-08-06 07:41:43