Java Blackberry中的Http连接问题

Java Blackberry中的Http连接问题,java,blackberry,Java,Blackberry,我正在开发一个应用程序,其中我需要通过POST方法传递参数,因此我尝试使用HTTPConnection实现,但它给我带来了错误。没有建立连接。在此代码中,我有带有URLEncodedPostData的pass参数,但它不能 这是我的密码: URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false); //passing q’s

我正在开发一个应用程序,其中我需要通过POST方法传递参数,因此我尝试使用
HTTPConnection
实现,但它给我带来了错误。没有建立连接。在此代码中,我有带有
URLEncodedPostData
的pass参数,但它不能

这是我的密码:

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);



    }

由于您想在连接中向发布一些参数,以下内容可能会有所帮助

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 + "";
这是将两个参数传递给webservice;用户名和密码。您可以相应地修改。连接扩展是指定连接类型的部分;BES、WIFI等。对于模拟器,您可以将其添加为
connectionString=“;deviceside=true”
。这利用了Ksoap2库,您可以通过属性->Java构建路径->库添加到项目中。“未建立连接”意味着设备无法使用选定的传输连接到服务器。请查看此答案:


建立连接后,Sarah关于传递参数和数据交换的帖子应该会有所帮助。

哪一行?请粘贴stacktrace。您可以尝试在Eclipse中使用它吗?我认为这是重复的。请添加任何连接参数以确定您在哪个网络中请求http请求。看看这个链接,你能根据问题给出答案吗?我已经添加了库,但是blackberry中的
HttpTransport
类呢?它不是派生的。你使用Ksoap库的HttpTransport。在项目中包含库(jar文件)时,可以导入org.ksoap2.transport.HttpTransport;你能给我一个下载HttpTransport jar的链接吗file@user1381267:你去吧!:)@Sarah我对库有问题,它与项目有关,但找不到Ksoap库。所以你能集中精力吗