1

Solution: Well, http request isn't allowed on android 4.0 because it'll a long time and ui will freeze. It's necessary to start a new thread.


first of all, this is my first post, so sorry if I do something wrong.

Well, I'm trying to get a list of venues from foursquare for my app. I've searched a lot but I still not found an answer. This is the first time I use foursquare api and json, but I don't know where is the problem or if I'm doing something wrong. The code fails when I try to get the stream from the url (.openStream()). I'm trying to get the venues list with userless request.

so this is my code, if someone could help me, I'll be so greatful! Thank you.

String url = "https://api.foursquare.com/v2/venues/search?intent=checkin&ll="+str+"&client_id="+client_id+"&client_secret="+client_secret+"&v="+currentDateandTime+"&limit=15";

try{

        URL urlend = new URL(url);
        URLConnection urlConnection = urlend.openConnection();
        InputStream in = (InputStream)urlend.openStream();
        String JSONReponse = CharStreams.toString(new InputStreamReader(in, "UTF-8"));

        JSONObject jsonObject = new JSONObject(JSONReponse);
        JSONObject reponseJson= jsonObject.getJSONObject("reponse");
        JSONArray arrayVenues= reponseJson.getJSONArray("venues");
        final String[] names= new String[15];
        final String[] venuesId= new String[15];

        for (int i=0;i< arrayVenues.length(); i++)
        {
            JSONObject jObj= arrayVenues.getJSONObject(i);
            names[i]= jObj.getString("name");
            venuesId[i]= jObj.getString("id");  
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
        listView.setAdapter(adapter);
        }

    } catch(Exception e){
        // In your production code handle any errors and catch the individual exceptions
        e.printStackTrace();
    }

EDIT: I tried other solution from here:

http://stackoverflow.com/questions/7281283/android-webrequest-simple-solution

and when it executes that instruction "response = httpclient.execute(httpget);" jumps to catch again. And in this one

HttpClient httpclient = new DefaultHttpClient();

when I debug and I focus in it appears this http://i44.tinypic.com/33nyscg.png

There is something wrong?

Thank you.

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
js_g
  • 71
  • 7
  • Is any exception thrown? – filipko May 01 '13 at 09:43
  • I don't see any error... The url from foursquare is correct? There's another way to get the json from foursquare? Thank you – js_g May 02 '13 at 09:42
  • Well, I keeps trying it, but everything I try fails when it tries to get the content from the url... I don't know what to do – js_g May 02 '13 at 10:01
  • Try to use debugger to find out the exact url string you open your URLConnection with. Maybe there is some typo. Everything works fine if I input my client ID and secret and random latitude and longitude. – filipko May 02 '13 at 13:35
  • Did you add any special permission? it could be something about that... I have permissions to internet and location, and I don't see any typo :S – js_g May 04 '13 at 16:42
  • Sorry, my answer was written in C# instead of Java :) I deleted it so you're not confused about that. You shouldn't need any special permissions. Do you know how to use debugger? Do you test your app on device emulator? – filipko May 05 '13 at 10:25
  • Well.. more or less I think... Where is it supposed to appear the error exactly? – js_g May 06 '13 at 19:42
  • Try to catch the value of string url before opening the URLConnection and check if it's in a correct format. If you don't know how to debug, you can print it out to the console (System.out). – filipko May 06 '13 at 19:47
  • Ohh yes, I had already done that. When I debug, the value of string url is this one: https:// api.foursquare.com/v2/venues/search?limit=15&intent=checkin&ll=37.357691,-5.9861981&client_id=(myclientid)&client_secret=(myclientsecret)&v=20130508" – js_g May 08 '13 at 08:55
  • (I had to separate https:// but in the string is together) – js_g May 08 '13 at 08:58
  • And when you open this URL in web browser, what's the response? Does the json contain data you are asking for? – filipko May 08 '13 at 10:12
  • yes, it does, but I'm not able to obtain that data – js_g May 08 '13 at 16:52
  • So if you open the URL in a browser, download the JSON file to your PC and open it in notepad, does it contain the data about venues in your location? – filipko May 08 '13 at 21:03
  • yes, or I think so, and if I put that url in foursquare "try it out" site, it works... – js_g May 09 '13 at 08:52
  • Well, debugging, I've just seen that the value of that instruction urlConnection = (HttpURLConnection) urlEnd.openConnection(); (connected) is false. Could be here the problem? – js_g May 09 '13 at 09:45
  • Now I noticed, that you don't use your `URLConnection` in your code any more after declaration. Try to change your declaration of `InputStream in` to `InputStream in = urlConnection.getInputStream();` – filipko May 09 '13 at 09:57
  • It continues doing nothing T_T – js_g May 09 '13 at 10:54
  • Then try to look into this question: http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java – filipko May 09 '13 at 10:57

1 Answers1

2

try this

private class GetChildList extends AsyncTask<String, Void, String>{

        private String strm = "22.81,89.55";
        private String client_id = "xxx";
        private String client_secret = "xxx";
        private String currentDateandTime = "20130715";  //yyyymmdd

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            DefaultHttpClient httpclient = new DefaultHttpClient();
            final HttpParams httpParams = httpclient.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            HttpConnectionParams.setSoTimeout(httpParams, 30000);
            HttpGet httppost = new HttpGet("https://api.foursquare.com/v2/venues/search?intent=checkin&ll="+strm+"&client_id="+client_id+"&client_secret="+client_secret+"&v="+currentDateandTime); //

            try{

                HttpResponse response = httpclient.execute(httppost);  //response class to handle responses
                jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

                JSONObject object = new JSONObject(jsonResult);   
             }
            catch(ConnectTimeoutException e){
                Toast.makeText(getApplicationContext(), "No Internet", Toast.LENGTH_LONG).show();
            }
            catch (ClientProtocolException e) {
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            return jsonResult;
        }

        protected void onPostExecute(String Result){
            try{

                Toast.makeText(getApplicationContext(), "R E S U L T :"+jsonResult, Toast.LENGTH_LONG).show();
                System.out.println(jsonResult);
                //showing result

            }catch(Exception E){
                Toast.makeText(getApplicationContext(), "Error:"+E.getMessage(), Toast.LENGTH_LONG).show();
            }

        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
             while ((rLine = rd.readLine()) != null) {
              answer.append(rLine);
               }
            }

            catch (IOException e) {
                e.printStackTrace();
             }
            return answer;
           }



    }
Mehdi
  • 540
  • 1
  • 11
  • 28