Java 世界时API调用启动onFailure而不是onSuccess
我正在尝试创建一个简单的应用程序,从一个API worldtimeapi中选择时间,然后单击一个按钮就显示出来。但是,当我单击时,onFailure方法被激活。我不明白为什么onSuccess似乎有问题。这是我的密码。我尝试通过ip地址选择时间,因为与使用区域选择时间相比,返回的JSON中的字段更容易解释 MainActivity.javaJava 世界时API调用启动onFailure而不是onSuccess,java,android,Java,Android,我正在尝试创建一个简单的应用程序,从一个API worldtimeapi中选择时间,然后单击一个按钮就显示出来。但是,当我单击时,onFailure方法被激活。我不明白为什么onSuccess似乎有问题。这是我的密码。我尝试通过ip地址选择时间,因为与使用区域选择时间相比,返回的JSON中的字段更容易解释 MainActivity.java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; i
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");
}
});
}
}
Logcat
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>
请检查您的清单文件是否需要添加internet权限您的url错误或是什么。它不起作用。 我试着通过ip地址选择时间 正如在文档中通过IP地址获取时间一样,需要在get请求中传递设备IP。但是您正在传递没有ip地址的BASE_URL来请求 按如下方式操作: 一,。获取设备IP地址: 二,。调用letsDoSomeNetworking时在基本URL中追加IP地址:
在onFailure中打印异常和响应,然后您可以获得错误的实际原因。您的url在浏览器中工作吗?url实际上不工作。这就是问题所在。把它改成正确的,没问题。非常感谢。这是有问题的url。谢谢。这是在我想获得任何设备的ip地址的情况下。我明白了逻辑。我试过了,但没有成功。我不明白我们为什么要在这部分加上双引号,/DEVICE\u IP\u ADDRESS。但是,不管有没有引号,代码都没有提取ip。这就是我如何从您提供的线程中提取ip的方法。WifiManager wifiMan=WifiManager getApplicationContext.getSystemServiceContext.WIFI_服务;WifiInfo wifiInf=wifiMan.getConnectionInfo;int-ipAddress=wifiInf.getIpAddress;字符串ip=字符串。格式%d.%d.%d.%d,ipAddress&0xff,ipAddress>>8&0xff,ipAddress>>16&0xff,ipAddress>>24&0xff;字符串finalUrl=BASE_URL+ip+.txt;
letsDoSomeNetworking(BASE_URL+"/"+<DEVICE_IP_ADDRESS>+".txt");