duandanxiu6965 2016-01-27 14:24
浏览 21
已采纳

Android - 如何从网址获取价值

I want to get a value from url in android.

For example:

In android i can write app id like this:

<string name="app_id">123456789012</string>

But i want to get app_id from url like this:

<string name="app_id">http://www.domain.com/index.php</string>

In the http://www.domain.com/index.php the only app_id is written 123456789012 and i want to retrieve this id in the string.

I search a lot on google and i found "android volley" but i'm understand how to use this.

String url = "http://httpbin.org/get?site=code&network=tutsplus";

JsonObjectRequest jsonRequest = new JsonObjectRequest
    (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            // the response is already constructed as a JSONObject!
            try {
                response = response.getJSONObject("args");
                String site = response.getString("site"),
                        network = response.getString("network");
                System.out.println("Site: "+site+"
Network: "+network);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

Volley.newRequestQueue(this).add(jsonRequest);
  • 写回答

2条回答 默认 最新

  • dongshao1156 2016-01-27 14:39
    关注

    Android Volley is a framework that allows you to make your HTTP request quite easy.

    When you trying to open the page in your browser, you are actually making HTTP request to some URL. All what you see than on the page - is returned in response to your request as html code or something else. So, if you want to retrive some data from URL you need to make http request to this url and parse response.

    How to do request with volley: you need to extend your own class from one of Request classes of volley, and than add it to request query (which means execution of request)

    public class GetRequest extends Request<String> {
    
    private Response.Listener<String> responseListener;
    
    public GetRequest (String login, String password) {
        super(Method.GET, http://www.domain.com/index.php,
        new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                    }
                });
    
        responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //parse your response here
            }
        };
    }
    
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = String.valueOf(new String(response.data, HttpHeaderParser.parseCharset(response.headers)));
        } catch (UnsupportedEncodingException e) {
            parsed = String.valueOf(new String(response.data));
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }
    
    @Override
    protected void deliverResponse(String response) {
        responseListener.onResponse(response);
    }
    }
    

    You should although notice, when you extending Request class, your specifying the type of expeted response. In current example it will be String.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R