1

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+"\nNetwork: "+network);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

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

Volley.newRequestQueue(this).add(jsonRequest);
Mykola
  • 3,343
  • 6
  • 23
  • 39

2 Answers2

0

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!

Community
  • 1
  • 1
caiolopes
  • 561
  • 8
  • 14
0

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.

Niakros
  • 236
  • 1
  • 3
  • 14