使用http get将值传递给url

时间:2011-11-17 06:50:01

标签: json http blackberry

我需要将2个字符串json参数传递给url,我需要获取json响应。如何在get方法中传递参数。这是我的代码

public void get()
{
     HttpConnection con = null;
     String url = "my url";

        try
        {
            URLEncodedPostData data = new URLEncodedPostData("UTF-8", false);
            data.append("method", "session.getToken");
            data.append("phonenumber:=", "1212345687");
            data.append("PIN:=", "1234");
            url = url +  data.toString();

            con = (HttpConnection)Connector.open(url);
            con.setRequestMethod(HttpConnection.GET);
            res = con.getResponseMessage();
            res1 = Integer.toString(con.getResponseCode());
            screen.add(new RichTextField("Reponce Message: "+res));
            screen.add(new RichTextField("Reponce Code: "+res1));


        }
        catch(Exception ex)
        {
            screen.add(new RichTextField(""+ex));

        }
}

1 个答案:

答案 0 :(得分:0)

根据您的服务器端语言,您可以通过将参数添加为字符串来传递json参数,例如

用于将参数传递给PHP服务器端

 String url = "http://www.test.com/test.php?";
 url += "method=" + session.getToken;
 url += "&phonenumber:=1212345687";
 url += "&PIN:=1234";

您的回复..试试这个

 public void get()
 {

    HttpConnection connection = null;
    String results;

    byte responseData[] = null;
    try { 
     connection = (HttpConnection) new                                           
        ConnectionFactory().getConnection(URL).getConnection();
        int len = (int) connection.getLength();
        responseData = new byte[len];
        DataInputStream dis;
        dis = new DataInputStream(connection.openInputStream());
        dis.readFully(responseData);
        results = new String(responseData);
        screen.add(new RichTextField("Reponce Message: "+results));
    } catch (Exception e) {
         // TODO Handle exception
         screen.add(new RichTextField(""+e));
    }
}