java - 世界时间 API 调用触发 onFailure 而不是 onSuccess

标签 java android

我正在尝试创建一个简单的应用程序,它从 API worldtimeapi 中选择时间,然后在单击按钮后显示它。但是,当我单击时,会启动 onFailure() 方法。我不明白为什么 onSuccess() 似乎有问题。这是我的代码。我尝试通过 IP 地址选择时间,因为与使用区域选择时间相比,返回的 JSON 中的字段更加不言自明。

MainActivity.java


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {
    final String BASE_URL = "http://worldtimeapi.org/api/timezone/ip";
    TextView holder;
    Button timeCollectButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        holder = findViewById(R.id.holder_text_view);
        timeCollectButton = findViewById(R.id.collect_button);

        timeCollectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Time", "Button has been clicked.");

                letsDoSomeNetworking(BASE_URL);
            }
        });



    }

    private void letsDoSomeNetworking(String url) {
        AsyncHttpClient client = new AsyncHttpClient();

        client.get(url, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d("Time", "Successful" + response.toString());



                try { // Try to...
                    Log.d("Time", "try block" + response.toString());
                    String timeHolder = response.getString("utc_datetime");
                    holder.setText(timeHolder);


                } catch (JSONException e) { // Catch a JSON exception if there is a problem parsing it
                    e.printStackTrace();
                }



            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {
                // The "onFailure()" method is called when response HTTP status is "4XX" (eg. 401, 403, 404)
                Log.d("Time", "Nothing Selected");
            }
        });
    }
}

日志猫

01-10 08:19:52.031 21703-21703/com.example.time D/Time: Button has been clicked.
01-10 08:19:52.596 21703-21703/com.example.time D/Time: Nothing Selected
01-10 08:19:55.168 21703-21703/com.example.time D/Time: Button has been clicked.
01-10 08:19:55.520 21703-21703/com.example.time D/Time: Nothing Selected

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/holder_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/holder_text_view"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/collect_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/collect_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/holder_text_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

你的网址是错误的还是什么。这不起作用。 enter image description here

关于java - 世界时间 API 调用触发 onFailure 而不是 onSuccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59676161/

相关文章:

java - 系统输出println

java - 如何在我的安卓设备上实现推送通知

java - 使用 SAX 解析 XML

java - 为什么在 Java 中使用不同的串联后比较字符串时会得到不同的结果?

java - 将 MongoDB 条目转换为 Java 类模型对象

java - 如何在 Java 的 HTTPS 请求调用中包含 SSL 证书文件

java - 如何自动更新 SharedPreferences 变量?

android - Gradle(Android)中每个构建变体的单独源文件?

android - 为什么 bindservice 在将其移动到工作线程后仍然会导致应用程序 ANR

java - 在一定条件下从firebase数据库中检索数据