小练手应用,练习了一下联网和json解析,如果哪里写的不好,还望指正!~~~~~~~~~~~
先上图片:
main.java
package com.example.weather;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import android.R.integer;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class Main extends ActionBarActivity {
static TextView TextView_CityName, TextView_Today_temp,
TextView_Today_weather, TextView_Today_tips,
TextView_tomorrom_temp, TextView_tomorrom_weather,
TextView_afterday_temp, TextView_afterday_weather, TextView_ptime;
EditText editText;
static Button button;
String city;
ImageView ImageView[]=new ImageView[3];
boolean rightJson=true,getpic=false;
Bitmap bitmap[]=new Bitmap[3];
JesonOut jesonOut = new JesonOut();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setlistener();
}
private void findViews() {
TextView_Today_temp = (TextView) findViewById(R.id.TextView_Today_temp);
TextView_CityName = (TextView) findViewById(R.id.TextView_CityName);
TextView_Today_weather = (TextView) findViewById(R.id.TextView_Today_weather);
TextView_Today_tips = (TextView) findViewById(R.id.TextView_Today_tips);
TextView_tomorrom_temp = (TextView) findViewById(R.id.TextView_tomorrom_temp);
TextView_tomorrom_weather = (TextView) findViewById(R.id.TextView_tomorrom_weather);
TextView_afterday_temp = (TextView) findViewById(R.id.TextView_afterday_temp);
TextView_afterday_weather = (TextView) findViewById(R.id.TextView_afterday_weather);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
ImageView[0]=(ImageView)findViewById(R.id.imageView_today);
ImageView[1]=(ImageView)findViewById(R.id.imageView_trrom);
ImageView[2]=(ImageView)findViewById(R.id.imageView_aftert);
}
private void setlistener() {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
city=editText.getText().toString();
Get get = new Get();
get.setCity(city);
Log.d("city_get",city);
Get.right=false;
getpic=false;
rightJson=true;
get.start();
while (true) {
if (Get.right) {
break;
}
}
{
try {
jesonOut.getOut(Get.result);
} catch (JSONException e) {
rightJson=false;
Toast.makeText(Main.this, "此城市无法查询!", Toast.LENGTH_LONG).show();
}
}
if(rightJson){
setText();
new Thread(new Runnable() {
@Override
public void run() {
getpic();
}
}).start();
while(!getpic){}
for (int i = 0;i<3;i++){
ImageView[i].setImageBitmap(bitmap[i]);
}
}
}
});
}
private void setText() {
TextView_CityName.setText(JesonOut.city);
TextView_Today_temp.setText(JesonOut.temp[0]);
TextView_Today_weather.setText(JesonOut.weather[0]);
TextView_Today_tips.setText(JesonOut.tips);
TextView_tomorrom_temp.setText(JesonOut.temp[1]);
TextView_tomorrom_weather.setText(JesonOut.weather[1]);
TextView_afterday_temp.setText(JesonOut.temp[2]);
TextView_afterday_weather.setText(JesonOut.weather[2]);
}
private void getpic () {
try{
for (int i=0;i<3;i++){
URL url=new URL(JesonOut.pic[i]);
bitmap[i]=BitmapFactory.decodeStream(url.openStream());
}
} catch (MalformedURLException e) {
Toast.makeText(Main.this, e.toString(), Toast.LENGTH_LONG).show(); //通知用户连接超时信息
}catch (IOException e) {
Toast.makeText(Main.this, e.toString(), Toast.LENGTH_LONG).show();
}
getpic=true;
}
}
Jeson.java(应该是json~~~~~懒得改了>。<)
package com.example.weather;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JesonOut {
static String city,
temp[]=new String[3],
weather[]=new String[3],
pic[]=new String[3],
status,pm=null,tips;
public void getOut(String json) throws JSONException {
JSONObject jsonObject=new JSONObject(json);
Log.d("json",json);
status=jsonObject.getString("status");
JSONArray resultJsonArray=jsonObject.getJSONArray("results");
JSONObject resultJsonObject=resultJsonArray.getJSONObject(0);
city=resultJsonObject.getString("currentCity");
pm=resultJsonObject.getString("pm25");
JSONArray indexJsonArray=resultJsonObject.getJSONArray("index");
tips = indexJsonArray.getJSONObject(0).getString("des");
JSONArray dataJsonArray=resultJsonObject.getJSONArray("weather_data");
for (int i=0;i<dataJsonArray.length()-1;i++){
JSONObject datajson=dataJsonArray.getJSONObject(i);
temp[i]=datajson.getString("temperature");
pic[i]=datajson.getString("dayPictureUrl");
weather[i]=datajson.getString("weather");
}
}
}
Get.java
package com.example.weather;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class Get extends Thread {
String strUrl;
public void setCity(String city) {
strUrl = "http://api.map.baidu.com/telematics/v3/weather?location="+city+"&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ";
Log.d("city_url",city);
Log.d("strUrl",strUrl);
}
static String result;
static boolean right=false;;
@Override
public void run() {
while (!right){
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(strUrl);
Log.d("strUrl2",strUrl);
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse;
try {
httpResponse = httpclient.execute(httpRequest);
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
result = EntityUtils.toString(httpResponse.getEntity());
right=true;
} else {
result = "get error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.d("result",result);
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.get_try.Main"
android:background="@drawable/green"
>
<TextView
android:id="@+id/TextView_Today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView_CityName"
android:layout_marginTop="16dp"
android:ems="10"
android:textColor="@color/wight"
android:text="@string/today" />
<TextView
android:id="@+id/TextView_tomorrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView_Today_temp"
android:layout_below="@+id/TextView_Today_tips"
android:layout_marginTop="26dp"
android:textColor="@color/wight"
android:ems="10"
android:gravity="left"
android:text="@string/tomorrom" />
<TextView
android:id="@+id/TextView_tomorrom_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView_tomorrom"
android:layout_below="@+id/TextView_tomorrom"
android:ems="10"
android:gravity="left"
android:textColor="@color/wight"
android:text="@string/wait" />
<TextView
android:id="@+id/TextView_tomorrom_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/TextView_tomorrom_temp"
android:layout_alignBottom="@+id/TextView_tomorrom_temp"
android:layout_alignParentRight="true"
android:ems="10"
android:gravity="right"
android:textColor="@color/wight"
android:text="@string/wait" />
<TextView
android:id="@+id/TextView_afterday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView_tomorrom_temp"
android:layout_marginTop="36dp"
android:ems="10"
android:textColor="@color/wight"
android:gravity="left"
android:text="@string/afterday" />
<TextView
android:id="@+id/TextView_afterday_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView_afterday"
android:ems="10"
android:textColor="@color/wight"
android:gravity="left"
android:text="@string/wait" />
<TextView
android:id="@+id/TextView_afterday_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/TextView_afterday_temp"
android:layout_alignBottom="@+id/TextView_afterday_temp"
android:layout_alignParentRight="true"
android:ems="10"
android:gravity="right"
android:textColor="@color/wight"
android:text="@string/wait" />
<TextView
android:id="@+id/TextView_Today_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView_Today"
android:layout_below="@+id/TextView_Today"
android:layout_marginTop="17dp"
android:ems="10"
android:text="@string/wait"
android:textColor="@color/wight" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入城市"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_toLeftOf="@+id/TextView_Today_weather"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView_Today_weather"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:text="@string/search" />
<ImageView
android:id="@+id/imageView_aftert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/TextView_afterday_temp"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/TextView_Today_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/TextView_Today_tips"
android:layout_alignParentRight="true"
android:ems="10"
android:gravity="right"
android:text="@string/wait"
android:textColor="@color/wight" />
<ImageView
android:id="@+id/imageView_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextView_Today_temp"
android:layout_alignLeft="@+id/imageView_aftert"
android:layout_centerHorizontal="true"
/>
<ImageView
android:id="@+id/imageView_trrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextView_tomorrom_temp"
android:layout_alignLeft="@+id/imageView_aftert"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/TextView_CityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button"
android:layout_marginTop="25dp"
android:ems="10"
android:gravity="center"
android:text="@string/wait"
android:textColor="@color/wight"
android:textSize="50sp" />
<TextView
android:id="@+id/TextView_Today_tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/TextView_Today_temp"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:ems="10"
android:gravity="center"
android:text="@string/wait"
android:textColor="@color/wight" />
<TextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextView_Today_tips"
android:layout_alignLeft="@+id/TextView_Today_temp"
android:layout_marginBottom="26dp"
android:ems="10"
android:text="Tips:"
android:textSize="30sp"
android:textColor="@color/blue" />
</RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Weather</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="today">今天</string>
<string name="tomorrom">明天</string>
<string name="afterday">后天</string>
<color name="cityname">#00FFFF</color>
<color name="wight">#FFFFFF</color>
<string name="wait">请稍后</string>
<string name="search">查询</string>
<color name="blue">#00B2EE</color>
</resources>