UrlConnection未获得JSON响应

时间:2013-07-14 09:26:01

标签: android multithreading urlconnection

大家好我想在按钮点击时使用活动中的线程打开一个URL。我用onClick方法准备一个线程。我写了代码。它正在工作,但作为回应我没有得到正确的回应。它应返回JSON响应,但返回半字符串。我的代码是

@Override
public void onClick(View arg0) {
    new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                url = new URL("https://api.metwit.com/v2/weather/?location_lat=22.03&location_lng=75.65");
                URLConnection conn = url.openConnection();
                BufferedReader br = new BufferedReader(
                           new InputStreamReader(conn.getInputStream()));

                String inputLine;
                while ((inputLine = br.readLine()) != null) {
                    s.append(inputLine);

                }
                //br.close();
                Log.i("####$$$", s.toString());
                JSONObject jsonObject = new JSONObject(s.toString());

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }).start();

}

我得到了像

这样的回复
{"objects": [{"weather": {"status": "partly cloudy", "measured": {"wind_speed": 2.056, "wind_direction": 240.0, "temperature": 316.0, "humidity": 8}}, "timestamp": "2013-07-13T08:07:47.397932", "sun_altitude": 1.309306025505066, "geo": {"type": "Point", "coordinates": [75.65, 22.03]}, "icon": "https://api.metwit.com/v2/icons/partly_sunny"}, {"sources": ["weatherbug", "wwo", "meteoblue"], "weather": {"status": "cloudy", "measured": {"wind_speed": 5, "wind_direction": 280, "temperature": 300, "humidity": 76}}, "timestamp": "2013-07-13T09:00:00", "sun_altitude": 1.0995509624481201, "geo": {"type": "Point", "coordinates": [75.5859375, 21.97265625]}, "icon": "https://api.metwit.com/v2/icons/cloudy"}, {"sources": ["weatherbug", "wwo", "meteoblue"], "weather": {"status": "rainy", "measured": {"wind_speed": 5, "wind_direction": 280, "temperature": 300, "humidity": 76}}, "timestamp": "2013-07-13T12:00:00", "sun_altitude": 0.38399845361709595, "geo": {"type": "Point", "coordin

我没有得到什么问题。

2 个答案:

答案 0 :(得分:2)

您可以使用droidQuery来处理所有事情:

$.with(myButton).click(new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        $.ajax(new AjaxOption().url("https://api.metwit.com/v2/weather/?location_lat=22.03&location_lng=75.65")
                               .type("GET")
                               .dataType("JSON")
                               .context(droidQuery.context())
                               .success(new Function() {
                                   @Override
                                   public void invoke($ droidQuery, Object... params) {
                                       JSONObject json = (JSONObject) params[0];
                                       droidQuery.toast(json.toString(), Toast.LENGTH_SHORT);
                                       //If you want to simplify parsing, consider using:
                                       Map<String, ?> data = $.map(json);
                                       //TODO continue parsing
                                   }
                               })
                               .error(new Function() {
                                   @Override
                                   public void invoke($ droidQuery, Object... params) {
                                       droidQuery.alert("ERROR: " + (String) params[2]);
                                   }
                               }));
    }
});

答案 1 :(得分:1)

您可以尝试使用android-query库,这很容易。

     AQuery aq = new AQuery(this);
     aq.ajax("https://api.metwit.com/v2/weather/?location_lat=22.03&location_lng=75.65",
             JSONObject.class,new AjaxCallback<JSONObject>(){

           @Override
           public void callback(String url, JSONObject object, AjaxStatus status) 
           {    
             String ret = object.toString();
             System.err.println(ret);
           }

     });

此解决方案适用于我。