0

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

b101
  • 379
  • 5
  • 15
  • 1
    In your ErrorListener add error.printstacktrace(); so that you get the reason for the error in your logcat – Jahnold Dec 18 '15 at 21:06
  • 1
    @Jahnold hmmm I don't understand the error, it says `java.net.ConnectException: failed to connect to /127.0.0.1 (port 3000) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)` that is strange, my REST API is up & running. I can open the ressource in my browser and get the JSON, as well as with CocaoRestClient EDIT: I tried localhost as well as 127.0.0.1 – b101 Dec 19 '15 at 15:27
  • searching for the error I found this answer: [link](http://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused) - but I still have to figure out if I have to change my port from 3000 to 8080 in order to make it work – b101 Dec 19 '15 at 15:35
  • Got it! I had to change the URL form `localhost` to `10.0.2.2`. Neither `localhost` or `127.0.0.1` is allowed since the simulator runs in a VM and `127.0.0.1` or `localhost` would create a backloop. so `10.0.2.2` is the correct IP address and my Port stayed the same ( in my case `3000`). Thank you @Jahnold for the clue – b101 Dec 19 '15 at 15:42

0 Answers0