public class MainActivity extends ActionBarActivity {
private Button button;
private TextView textView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
String re = (String) msg.obj;
textView.setText(re);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttsearch);
textView = (TextView) findViewById(R.id.show_tv);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "点击了Button");
sendRequestWithHttpClient();
}
});
}
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8D%97%E5%AE%81&output=json&ak=XXupCGKz1NrHH0DwIwKC74GWspa1bVGS");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Log.i("TAG", response.toString()); parseJSONObjectOrJSONArray(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONObjectOrJSONArray(String jsonData) {
try {
String count = "";
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("results");
if (jsonArray.length() > 0) {
JSONObject object = jsonArray.getJSONObject(0);
String city = object.optString("currentCity");
JSONArray array = object.getJSONArray("weather_data");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject1 = array.getJSONObject(i);
String dateDay = jsonObject1.optString("date");
String weather = jsonObject1.optString("weather");
String wind = jsonObject1.optString("wind");
String temperature = jsonObject1.optString("temperature");
count =count +"\n"+ dateDay + " " + weather + " " + wind + " " + temperature;
Log.i("AAA",count);
}
Message message = new Message();
message.what = 0;
message.obj = count;
handler.sendMessage(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private Button button;
private TextView textView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
String re = (String) msg.obj;
textView.setText(re);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttsearch);
textView = (TextView) findViewById(R.id.show_tv);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "点击了Button");
sendRequestWithHttpClient();
}
});
}
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8D%97%E5%AE%81&output=json&ak=XXupCGKz1NrHH0DwIwKC74GWspa1bVGS");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Log.i("TAG", response.toString()); parseJSONObjectOrJSONArray(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONObjectOrJSONArray(String jsonData) {
try {
String count = "";
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("results");
if (jsonArray.length() > 0) {
JSONObject object = jsonArray.getJSONObject(0);
String city = object.optString("currentCity");
JSONArray array = object.getJSONArray("weather_data");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject1 = array.getJSONObject(i);
String dateDay = jsonObject1.optString("date");
String weather = jsonObject1.optString("weather");
String wind = jsonObject1.optString("wind");
String temperature = jsonObject1.optString("temperature");
count =count +"\n"+ dateDay + " " + weather + " " + wind + " " + temperature;
Log.i("AAA",count);
}
Message message = new Message();
message.what = 0;
message.obj = count;
handler.sendMessage(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
****************************************
我轻狂太高傲我懵懂无知太年少
****************************************