首页 > 解决方案 > 在 Android Studio 中,我可以使用 api 同步数据

问题描述

在 Android Studio 中,我无法使用 api 同步数据。由于我的代码是异步的,所以当我第二次按下按钮时它就会出现。

我想做的是使用api同步获取数据。

在此处输入图像描述 我的代码:

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private CustomWebViewClient webViewClient;
    private String Url = "";
    private String LiveBroadCastUrl;
    ProgressDialog mProgressDialog;
    RelativeLayout relativeLayout;
    RelativeLayout permissionsMedia;
    Button btnNoInternetConnections;
    Button permissionsButton;
    Button refresPermissons;
    ImageView permissionsLogo;
    ImageView noInternetLogo;
    RequestQueue mQueue;
    String time;
    String responsestate;
    Context context=this;
    //public Context context;
    public static int REQUEST_PERMISSION=1;
    private static final String TAG = MainActivity.class.getSimpleName();

    private static final int FILECHOOSER_RESULTCODE = 1;
    private ValueCallback<Uri> mUploadMessage;
    private Uri mCapturedImageURI = null;

    // the same for Android 5.0 methods only
    private ValueCallback<Uri[]> mFilePathCallback;
    private String mCameraPhotoPath;
    ProgressBar progressBarWeb;
    private String sURL,sFileName,sUser;

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
}

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                        try {
                            time = jsonObject.getString("CustomObject");
                            responsestate= jsonObject.getString("CustomResponseState");
                            //Log.e("gelen değer",time);
                        } catch (JSONException e) {
                            //Log.e("hata","catche düştü");
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(context, "Hata Oluştu", Toast.LENGTH_SHORT).show();
                    }
                });
                mQueue.add(request);
}

标签: javaandroidapiandroid-studio

解决方案


推荐阅读