直接上图:
在这里,先来提供天气接口:
http://wthrcdn.etouch.cn/weather_mini?city=
在city后面需要查询的城市即可,这是JSON数据,需要解析自己需要的信息。
上次已经说到了如何解析,现在直接附代码:
package com.example.weathertest2;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Request.Builder;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText main_et;
private TextView wendu,tishi,fengxiang,fengli,high,type,low,date;
private ProgressDialog pd;
private String tianqi[];
private MediaPlayer player;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==10)
{
pd.dismiss();
String content=(String) msg.obj;
try {
tianqi=getJieXi(content);
wendu.setText("当前温度: \t"+tianqi[0]);
tishi.setText("温馨提示: \t"+tianqi[1]);
fengxiang.setText("风向: \t"+tianqi[2]);
fengli.setText("风力: \t"+tianqi[3]);
high.setText("最"+tianqi[4]);
type.setText("天气类型: \t"+tianqi[5]);
low.setText("最"+tianqi[6]);
date.setText("日期: \t"+tianqi[7]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initViews();
player=MediaPlayer.create(this, R.raw.wangtian);
player.start();
}
protected String[] getJieXi(String content) throws Exception {
// TODO Auto-generated method stub
JSONObject obj=new JSONObject(content);
JSONObject data = obj.getJSONObject("data");
String wendu = data.getString("wendu");
String tishi=data.getString("ganmao");
JSONArray forecast = data.getJSONArray("forecast");
JSONObject obj2=(JSONObject) forecast.get(0);
String fengxiang=obj2.getString("fengxiang");
String fengli=obj2.getString("fengli");
String high=obj2.getString("high");
String type=obj2.getString("type");
String low=obj2.getString("low");
String date=obj2.getString("date");
String tianqi[]={wendu,tishi,fengxiang,fengli,high,type,low,date};
return tianqi;
}
private String getCity() {
// TODO Auto-generated method stub
String city=main_et.getText().toString().trim();
return city;
}
private void initViews() {
// TODO Auto-generated method stub
main_et=(EditText) findViewById(R.id.main_et);
wendu =(TextView) findViewById(R.id.wendu);
tishi =(TextView) findViewById(R.id.tishi);
fengxiang =(TextView) findViewById(R.id.fengxiang);
fengli =(TextView) findViewById(R.id.fengli);
high =(TextView) findViewById(R.id.high);
type =(TextView) findViewById(R.id.type);
low=(TextView) findViewById(R.id.low);
date =(TextView) findViewById(R.id.date);
}
public void myclick(View v) throws Exception
{
pd=new ProgressDialog(this);
pd.setMessage("正在查询,请稍候...");
pd.show();
OkHttpClient client=new OkHttpClient();
Request.Builder builder=new Builder();
builder.get();
builder.url("http://wthrcdn.etouch.cn/weather_mini?city="+URLEncoder.encode(getCity(), "UTF-8"));
Request request = builder.build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onResponse(Response arg0) throws IOException {
// TODO Auto-generated method stub
String content=arg0.body().string();
Message msg=Message.obtain();
msg.obj=content;
msg.what=10;
handler.sendMessage(msg);
}
@Override
public void onFailure(Request arg0, IOException arg1) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(player!=null)
{
player.reset();
player.release();
}
}
}
在这里加入了多媒体的知识,添加了背景音乐,之后会慢慢提到,这个不影响当前代
码。
URLEncoder.encode(getCity(), “UTF-8”))这里用到的这个方法是将输入的中文转换
为UTF-8的编码方式,不然会出现乱码。
写到这里,可以去装装逼啊!~~~