【问题标题】:how to parse json response from webservice url and parse it as url in android如何解析来自webservice url的json响应并将其解析为android中的url
【发布时间】:2013-04-01 17:44:25
【问题描述】:

我有一个应用程序,我使用 this link 调用 webservice,我有一个 webservice Url,另一个 Url 正在从该 url 获得响应。我需要将该 url 用作

public static final String TIME_CENTRAL_SERVER = "http://accounts.myexample.com/Services";代替 “http://accounts.myexample.com/Services”我需要解析我的 json 响应。

我已经在谷歌中检查过,但无法得到任何答案,谁能帮助我解决这个问题,在此先感谢。 如果有人有疑问可以问我。

【问题讨论】:

    标签: android json web-services request


    【解决方案1】:

    第一次 web 服务调用如下所示

    RestClient client = new RestClient(LOGIN_URL);
    client.AddParam("accountType", "GOOGLE");
    client.AddParam("source", "tboda-widgalytics-0.1");
    client.AddParam("Email", _username);
    client.AddParam("Passwd", _password);
    client.AddParam("service", "analytics");
    client.AddHeader("GData-Version", "2");
    
    try {
        client.Execute(RequestMethod.POST);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    String response = client.getResponse();
    

    解析响应后,如果你想再次调用Web Service,只需创建另一个具有不同URL和参数的RestClient对象并调用execute方法,如下所示,

    RestClient client1 = new RestClient(GET_INFO_URL);
    client1.AddParam("userid", "123");
    
    try {
        client1.Execute(RequestMethod.POST);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    String response1 = client1.getResponse();
    

    【讨论】:

    • 以上回复不是我想要的
    • @Html5 - 您能否提供第一个休息请求的回复?然后你想对该请求做什么?
    • 我得到一个 url 作为来自 web 服务请求的响应,我需要附加该 url @Amit
    • 你的意思是 String response = client.getResponse();将返回一个 URL?如果是这种情况,那么在后续的 Rest Request 中,在 RestClient 构造函数中提供“response”为“RestClient client1 = new RestClient(response);”
    • 是的,我从 web 服务 url 获取字符串作为响应,你能告诉我如何跟进代码
    【解决方案2】:

    最后我在团队负责人的指导下解决了我的问题,下面是我们在 constants.java 类中使用的代码

    public static final String GET_CENTRAL_SERVER = String.format("%s/AccountService/security/ValidateAccess", TIMEMACHINE_ACCOUNTS_SERVER);
    

    并在 serversync.java 类中添加代码 sn-p

    public String getCentralServer(Context context, String serial_number) throws Exception{
        // TODO Auto-generated method stub
        WebServiceClient client = new WebServiceClient(Constants.GET_CENTRAL_SERVER);
        client.addParam("accesscode", String.valueOf(serial_number));
        client.addParam("type", "2");
        client.Execute(RequestMethod.GET);
        String response = client.getResponse();
        if (response != null){
            response = response.replaceAll("\\\\/", "/");
            response = response.replace("\"", "");
            response = response.replace("\n","");
            response = "http://" + response;
            return response;
        }
    
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 2013-03-23
      • 1970-01-01
      • 2015-08-05
      • 2012-11-23
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      相关资源
      最近更新 更多