java - 如何获取四方 field 列表 - Android

标签 java android json foursquare

解决方案:嗯,http请求在android 4.0上是不允许的,因为它会很长时间并且ui会卡住。有必要开始一个新线程。


首先,这是我的第一篇文章,如果我做错了什么,请见谅。

好吧,我正尝试从 foursquare 为我的应用程序获取 field 列表。我已经搜索了很多,但仍然没有找到答案。这是我第一次使用foursquare api和json,但我不知道问题出在哪里,或者我做错了什么。当我尝试从 url (.openStream()) 获取流时,代码失败。我正在尝试通过无用户请求获取 field 列表。

所以这是我的代码,如果有人能帮助我,我将非常感激!谢谢。

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();
    }

编辑: 我从这里尝试了其他解决方案:

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

当它执行指令时“response = httpclient.execute(httpget);”跳起来再次捕获。而在这一个

HttpClient httpclient = new DefaultHttpClient();

当我调试并专注于它时,会出现这个 http://i44.tinypic.com/33nyscg.png

有什么问题吗?

谢谢。

最佳答案

试试这个

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;
           }



    }

关于java - 如何获取四方 field 列表 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16315854/

相关文章:

Java 中的 JavaScript 对象?

java - 发现多个文件的操作系统独立路径为 'META-INF/androidx.legacy_legacy-support-core-utils.version'

android - 完整的Android对OSGi bundle 的支持

mysql - 如何为Mysql 5.7选择具有这种结构的json列

javascript - 按 Asc 顺序字符串化 JS 对象

java - 涉及多个数据源之间交互的设计模式

java - 公钥与 RSAPublicKeySpec

java - 同一实体的并发更新

android - Java - 将 16 位 Mono PCM 原始数据转换为立体声

c# - 如何修改反序列化对象