简体   繁体   English

从URL加载JSON数据时如何显示加载(圆圈)

[英]how show loading (circle) when loading JSON data from an URL

I am new to Android Studio and I am trying to make my simple app get JSON data from an URL using Volley. 我是Android Studio的新手,我正在尝试使我的简单应用使用Volley从URL获取JSON数据。
Everything is fine, but I want to I want to display a loading circle when it gets the JSON data. 一切都很好,但是我想在获取JSON数据时显示一个加载圆圈。

my code if any one can help me 我的代码(如果有人可以帮助我)

package imo.meteoiraq;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.Timestamp;

public class MainActivity extends AppCompatActivity {
    RequestQueue rq;
    TextView timeDesc, tempDesc, windspeedDesc, windguestDesc, humdityDesc;
    int ages;
    int temp;
    int windspeed;
    int windguest;
    int humdity;
    long timeupdate;

    String url = "stationlookup?station=I1410&units=metric&v=2.0&format=json";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rq = Volley.newRequestQueue(this);

        timeDesc = (TextView) findViewById(R.id.timeupdateDesc);
        tempDesc = (TextView) findViewById(R.id.tempid);
        windspeedDesc = (TextView) findViewById(R.id.windid);
        windguestDesc = (TextView) findViewById(R.id.windgustid);
        humdityDesc = (TextView) findViewById(R.id.humdid);

        sendjsonrequest();
    }

    public void sendjsonrequest() {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    JSONObject stationsJO = response.getJSONObject("stations");
                    JSONObject I1410JO = stationsJO.getJSONObject("I1410");
                    temp = I1410JO.getInt("temperature");
                    windspeed = I1410JO.getInt("wind_speed");
                    windguest = I1410JO.getInt("wind_gust_speed");
                    humdity = I1410JO.getInt("humidity");
                    timeupdate = I1410JO.getLong("updated") * 1000L;

                    tempDesc.setText(Integer.toString(temp));
                    windspeedDesc.setText(Integer.toString(windspeed));
                    windguestDesc.setText(Integer.toString(windguest));
                    humdityDesc.setText(Integer.toString(humdity));
                    timeDesc.setText(getDate(timeupdate));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        rq.add(jsonObjectRequest);
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                sendjsonrequest();
                handler.postDelayed(this, 6000);//60 second delay
            }
        };
        handler.postDelayed(runnable, 1000);
    }

    private String getDate(long timeStamp) {
        try {
            DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Date netDate = (new Date(timeStamp));
            return sdf.format(netDate);
        } catch (Exception ex) {
            return "xx";
        }
    }


    }

All you need is a ProgressBar in your layout. 您需要的只是布局中的ProgressBar You can set the visibility as invisible at the beginning. 您可以在开始时将可见性设置为invisible Then when you start parsing set the visibility to visible and when you finish to invisible again. 然后,当你开始解析设置能见度visible ,当你完成对invisible一次。 I think this is the easiest way to do it. 我认为这是最简单的方法。

Try this: 尝试这个:

package imo.meteoiraq;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.Timestamp;

public class MainActivity extends AppCompatActivity {
    RequestQueue rq;
    TextView timeDesc, tempDesc, windspeedDesc, windguestDesc, humdityDesc;
    int ages;
    int temp;
    int windspeed;
    int windguest;
    int humdity;
    long timeupdate;

    String url = "stationlookup?station=I1410&units=metric&v=2.0&format=json";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rq = Volley.newRequestQueue(this);

        timeDesc = (TextView) findViewById(R.id.timeupdateDesc);
        tempDesc = (TextView) findViewById(R.id.tempid);
        windspeedDesc = (TextView) findViewById(R.id.windid);
        windguestDesc = (TextView) findViewById(R.id.windgustid);
        humdityDesc = (TextView) findViewById(R.id.humdid);

        sendjsonrequest();
    }

    public void sendjsonrequest() {
 final ProgressDialog dialog = ProgressDialog.show(this, null, "Please Wait");
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
 dialog.dismiss();
                try {

                    JSONObject stationsJO = response.getJSONObject("stations");
                    JSONObject I1410JO = stationsJO.getJSONObject("I1410");
                    temp = I1410JO.getInt("temperature");
                    windspeed = I1410JO.getInt("wind_speed");
                    windguest = I1410JO.getInt("wind_gust_speed");
                    humdity = I1410JO.getInt("humidity");
                    timeupdate = I1410JO.getLong("updated") * 1000L;

                    tempDesc.setText(Integer.toString(temp));
                    windspeedDesc.setText(Integer.toString(windspeed));
                    windguestDesc.setText(Integer.toString(windguest));
                    humdityDesc.setText(Integer.toString(humdity));
                    timeDesc.setText(getDate(timeupdate));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
 dialog.dismiss();
            }
        });
        rq.add(jsonObjectRequest);
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                sendjsonrequest();
                handler.postDelayed(this, 6000);//60 second delay
            }
        };
        handler.postDelayed(runnable, 1000);
    }

    private String getDate(long timeStamp) {
        try {
            DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Date netDate = (new Date(timeStamp));
            return sdf.format(netDate);
        } catch (Exception ex) {
            return "xx";
        }
    }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM