I am trying to get JSON response from a URL. I have tried below code several time but i couldn't get the response. Please Help me to sort this out
I am using API level 29
Here i am using this url for getting data, url -> https://api.androidhive.info/contacts/
My Code:
public class MainActivity extends AppCompatActivity {
private final static String URL = "https://api.androidhive.info/contacts/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest arrayRequest = new JsonArrayRequest(
Request.Method.GET, URL,null,new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("Response : ", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error : ", error.getMessage());
}
});
queue.add(arrayRequest);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleyparsing">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The Logcat response:
2020-05-03 22:19:49.600 11515-11515/? I/e.volleyparsin: Not late-enabling -Xcheck:jni (already on)
2020-05-03 22:19:49.749 11515-11515/? E/e.volleyparsin: Unknown bits set in runtime_flags: 0x8000
2020-05-03 22:19:53.066 11515-11515/com.example.volleyparsing D/Volley: [2] 2.onErrorResponse: Error :
2020-05-03 22:19:50.513 11515-11545/com.example.volleyparsing W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
Your response is jsonObject not jsonArray. That's way you are getting parsing error. Use jsonObject:
RequestQueue queue = Volley.newRequestQueue(context);
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray = null;
try {
jsonArray = response.getJSONArray("contacts");
Log.d("Response : ", jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error : ", error.getMessage());
}
});
queue.add(objectRequest);
Use android:usesCleartextTraffic="true" on your manifest file on application.
<application
...
android:usesCleartextTraffic="true"
... >
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.