I tried a Volley tutorial for parsing JSONObjects and it worked wonderfully, so I wanted to try to use a Volley JSONAarrayRequest on my localhost running a REST API. My Ressource (url) returns a JSON Array with the following structure:
[
{
"Latitude": 50.995739999999998,
"Stadt": "Köln",
"Beschreibung": "DRK",
"Longitude": 6.9249700000000001,
"Adresse": "Altonaer Str. 32",
"SRID": 4,
"Materialien Anzahl": 45,
"PLZ": "50737"
},
{
"Latitude": 50.997259999999997,
"Stadt": "Köln",
"Beschreibung": "Schreibwarenhandel Müller",
"Longitude": 6.9046219999999998,
"Adresse": "Grethenstraße 9",
"SRID": 3,
"Materialien Anzahl": 25,
"PLZ": "50739"
},
{
"Latitude": 50.998410999999997,
"Stadt": "Köln",
"Beschreibung": "Kath. Kirchen e.V",
"Longitude": 6.9066720000000004,
"Adresse": "Heimersdorfer Strasse 7",
"SRID": 1,
"Materialien Anzahl": 20,
"PLZ": "50739"
}
]
My Java Code for the request looks like this:
requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest localJReq = new JsonArrayRequest(localURL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response){
try{
for (int i=0; i < response.length(); i++)
{
JSONObject jsonObject = response.getJSONObject(i);
String title = jsonObject.getString("Beschreibung");
String addres = jsonObject.getString("Adresse");
String laengen = jsonObject.getString("Longitude");
String breiten = jsonObject.getString("Latitude");
data += "Lagerstandort "+(i+1)+"\nBeschreibung: "+title+"\nAdresse: "+addres+"\nLängengrad: "+laengen+"\nBreitengrad: "+breiten+"\n\n\n";
}
output.setText(data);
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MainActivity.this, "No more Items Available", Toast.LENGTH_LONG).show();
}
});
requestQueue.add(localJReq);
All of this is happening inside my onCreate()
and of course output is my TextView output = (TextView) findViewById(R.id.jsonData);
- it already worked in the tutorial I rebuild, so it shouldn't be a problem.
When I run my Application I get the onErrorResponse, throwing the Toast message Toast.makeText(MainActivity.this, "No more Items Available", Toast.LENGTH_LONG).show();
My REST API is up and running 100%, so it shouldn't be a problem either.
my sdk settings and dependencies are
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.b101.closestapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile project(':volley')
}
My guess would be that my JSONArrayRequest isn't done right? But I'm not sure..
Thank you