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
什麼是錯的我沒有得到。
你有沒有嘗試在您的瀏覽器連接的URL。迴應是什麼? – Tugrul
是的,我嘗試在瀏覽器中得到適當的迴應。 –
我也嘗試在不同的java項目中使用java代碼,我得到了適當的迴應。 –