【问题标题】:passing values to url using http get使用 http get 将值传递给 url
【发布时间】:2011-11-17 06:50:00
【问题描述】:

我需要将 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));

        }
}

【问题讨论】:

  • 运行此代码时服务器上会发生什么?你期望发生什么?
  • @Michael Donohue:它将返回一个字符串。我需要得到它

标签: json http blackberry


【解决方案1】:

根据您的服务器端语言,您可以通过将参数添加为字符串来传递 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));
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2023-03-09
    • 2021-06-28
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2014-09-25
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多