如何解析android中的url

时间:2015-04-05 10:25:48

标签: java android json sqlite

我有一个URL应该解析值,但是当我给出

private static String url = "http://54.174.74.84/api/index/index?data={%20%22language_code%22:%22en_us%22,%20%22cmd%22:%22search_projects%22,%20%22user_device_id%22:319,%20%22page%22:0,%20%22user_token%22:%22edeN2y0EuakoD2deWGzijeuBQ5HRnhokyAqV4WIHzZz5PM0Qn2xgopKiAcboNJktgXeQMsX7kZmDW3T5Tta3i+Fz1mU5p1yTP1L3m\/OTGkOlDoJrLn6\/+I3rBqESDtFH%22,%20%22source_app%22:%22android%22,%20%22longitude%22:%22567%22,%20%22user_id%22:67,%20%22latitude%22:%22123%22,%20%22records_per_page%22:%2210%22,%20%22search_key%22:%22%22%20}";
像这样它显示错误。所以请帮助我。感谢。

2 个答案:

答案 0 :(得分:1)

这不是错误。只是用java编码器类进行字符串编码。您必须在使用它之前解码字符串,如下面的代码...

String url = "http://54.174.74.84/api/index/index?data={%20%22language_code%22:%22en_us%22,%20%22cmd%22:%22search_projects%22,%20%22user_device_id%22:319,%20%22page%22:0,%20%22user_token%22:%22edeN2y0EuakoD2deWGzijeuBQ5HRnhokyAqV4WIHzZz5PM0Qn2xgopKiAcboNJktgXeQMsX7kZmDW3T5Tta3i+Fz1mU5p1yTP1L3m/OTGkOlDoJrLn6/+I3rBqESDtFH%22,%20%22source_app%22:%22android%22,%20%22longitude%22:%22567%22,%20%22user_id%22:67,%20%22latitude%22:%22123%22,%20%22records_per_page%22:%2210%22,%20%22search_key%22:%22%22%20}";

String result = java.net.URLDecoder.decode(url, "UTF-8");

答案 1 :(得分:0)

您需要使用Uri类:

String uri = Uri.parse("http://54.174.74.84/api/index/index")
            .buildUpon()
            .appendQueryParameter("data", "{%20%22language_ ..... %22%22%20}")
            .appendQueryParameter("param2", value2)
            .appendQueryParameter("param3", value3)
            .build().toString();