2013-07-14 110 views
1

大家好,我正在嘗試打開一個URL使用按鈕點擊活動中的線程。我在onClick方法中準備一個線程。我寫了代碼。它正在工作,但作爲迴應,我沒有得到正確的答覆。它應該返回JSON響應,但它返回的一半字符串。我的代碼是UrlConection沒有得到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 

什麼是錯的我沒有得到。

+0

你有沒有嘗試在您的瀏覽器連接的URL。迴應是什麼? – Tugrul

+0

是的,我嘗試在瀏覽器中得到適當的迴應。 –

+0

我也嘗試在不同的java項目中使用java代碼,我得到了適當的迴應。 –

回答

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

    }); 

此解決方案對我來說工作正常。

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