大家好,我正在尝试使用按钮单击活动中的线程打开一个 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
我没有得到什么是错的。