1

大家好,我正在尝试使用按钮单击活动中的线程打开一个 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

我没有得到什么是错的。

4

2 回答 2

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]);
                                   }
                               }));
    }
});
于 2013-07-17T03:25:21.127 回答
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);
           }

     });

这个解决方案对我来说很好。

于 2013-07-14T14:57:35.553 回答