我想从我的网址获取一个简单的字符串。这是我的网址: http://generalbackend.appsazan.com/WeatherAPI.svc/lastUpdate 这将返回一个字符串,如下所示:" 6/2/2018 2:22:22 PM" 如何获取它并在textview中显示。 任何解决方案?
答案 0 :(得分:0)
您必须进行HTTP GET调用才能获得此字符串。您可以在android
中使用改装或排球或简单的HTTPConnection这是一个示例代码
final TextView mTextView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);