duandanxiu6965
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);

图片转代码服务由CSDN问答提供 功能建议

我想从android中的url获取一个值。

例如 :

在android中,我可以像这样编写app id:

 &lt; string name =“app_id”&gt; 123456789012&lt; / string&gt  ; 
   
 
 

但我想从url获取 app_id ,如下所示:

   &lt; string name =“app_id”&gt; http://www.domain.com/index.php< / string&gt; 
   
 
 

http://www.domain.com/index.php 只写 app_id 123456789012 我想在字符串中检索此ID。

我在google上搜索了很多,我发现了“android volley”,但我知道如何使用它。

  String url =“http://httpbin.org/get?site=code&network=tutsplus";
nnJsonObjectRequest jsonRequest = new JsonObjectRequest 
(Request.Method.GET  ,url,null,new Response.Listener&lt; JSONObject&gt;(){
 @Override 
 public void  onResponse(JSONObject响应){
 //响应已构造为JSONObject!
尝试{
 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  (); 
} 
} 
},新的Response.ErrorListener(){
 
 @Override 
 public void onErrorResponse(VolleyError error){
 error.printStackTrace(); 
} 
}  ); 
 
Volley.newRequestQueue(this).add(jsonRequest); 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshao1156
    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.

    点赞 评论
  • dongshao8471
    dongshao8471 2016-01-27 14:37

    As answered by Egor in this question:

    Changing value of R.String Programmatically

    You can't change strings.xml dynamically.

    The way you should do it is to retrieve the data from http://www.domain.com/index.php for example using Volley library (http://developer.android.com/training/volley/index.html), Retrofit (http://square.github.io/retrofit/), or even an AsyncTask.

    Finally, you need to parse the content (if it is an XML or JSON for example) in order you can use it in your app.

    It all sounds very complicated but you can start looking tutorials like this one http://www.vogella.com/tutorials/Retrofit/article.html

    Hope it helps!

    点赞 评论

相关推荐