问题描述
我正在开发一个 Android 应用程序,其中使用了 JsonObjectRequest
和 Android 的 Volley。
我的应用程序似乎在 Android 的 Pixel XL API 30 上运行得非常好(打印出所需的输出),但不适用于 Android 的 Pixel 2 API 29,即使我在 {{ 中定义了 minSdkVersion 16
1}}
这是我的build.gradle
:
build.gradle
这是我的plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.krish.parsedata"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.android.volley:volley:1.2.0'
}
:
MainActivity.java
谢谢!
更新:这一直显示:
public class MainActivity extends AppCompatActivity {
RequestQueue queue;
String url = "https://www.google.com";
String apiUrl = "https://jsonplaceholder.typicode.com/todos";
String getApiUrl = "https://jsonplaceholder.typicode.com/todos/1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,getApiUrl,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("url","onCreate: " + response.getString("title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("url2","There was an error");
}
});
queue.add(jsonObjectRequest);
}
}
清单:
Android Studio is using the following JDK location when running Gradle:
/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home
Using different JDK locations on different processes might cause Gradle to
spawn multiple daemons,for example,by executing Gradle tasks from a terminal
while using Android Studio.
更新:它适用于所有 API 30 级别的设备,但不适用于更低的设备,我尝试更改最小 sdk 版本、编译 sdk 版本和构建 sdk 版本,但不走运。不知道该怎么办。帮助将不胜感激。谢谢。
解决方法
我发现了错误。问题与我的代码或构建配置无关,而是模拟器本身。我删除/重新安装了模拟器,一切正常。
希望这能帮助任何遇到同样问题的人。