-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #982 from YLiuLiu/master
- Loading branch information
Showing
4 changed files
with
296 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...ain/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.example.test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import org.json.JSONObject; | ||
|
||
|
||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.app.Activity; | ||
import android.view.Menu; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
public class Com1314080901239GetJson extends Activity { | ||
|
||
private final int SUCCESS = 1; | ||
private final int FAILURE = 0; | ||
private final int ERRORCODE = 2; | ||
private TextView tv; | ||
|
||
private Handler handler=new Handler(){ | ||
public void handleMessage(Message msg){ | ||
switch (msg.what) { | ||
case SUCCESS: | ||
String result=getJson(msg.obj.toString()); | ||
//主线程更新UI | ||
tv.setText(result); | ||
Toast.makeText(Com1314080901239GetJson.this, "获取数据成功", Toast.LENGTH_SHORT) | ||
.show(); | ||
break; | ||
case FAILURE: | ||
Toast.makeText(Com1314080901239GetJson.this, "获取数据失败", Toast.LENGTH_SHORT) | ||
.show(); | ||
case ERRORCODE: | ||
Toast.makeText(Com1314080901239GetJson.this, "获取的CODE码不为200!", | ||
Toast.LENGTH_SHORT).show(); | ||
default: | ||
break; | ||
} | ||
} | ||
}; | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_com1314080901239_get_json); | ||
tv=(TextView) findViewById(R.id.tv); | ||
} | ||
|
||
public String getJson(String string){ | ||
JSONObject jObject= null; | ||
try { | ||
jObject =new JSONObject(string); | ||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
System.out.println("创建JsonObject失败"); | ||
} | ||
//解析JSON数据 | ||
String city=jObject.optString("city"); | ||
String city_en=jObject.optString("city_en"); | ||
String date_y=jObject.optString("date_y"); | ||
String week=jObject.optString("week"); | ||
int cityid=jObject.optInt("cityid"); | ||
String temp1=jObject.optString("temp1"); | ||
System.out.println(temp1+""+date_y+""+city_en+""+cityid+""+week+""+city); | ||
String weatherResult="城市:"+city+"\n城市英文名:"+city_en+"\n日期:"+date_y+"\n星期:"+week+"\n城市编号:"+cityid+"\n温度:"+temp1; | ||
return weatherResult; | ||
} | ||
|
||
public void click(View v){ | ||
//开子线程获取网络请求 | ||
new Thread(){ | ||
int code=0; | ||
public void run() { | ||
// TODO Auto-generated method stub | ||
try { | ||
|
||
String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/city.json"; | ||
URL url=new URL(path); | ||
HttpURLConnection conn=(HttpURLConnection) url.openConnection(); | ||
//设置GET请求 | ||
conn.setRequestMethod("GET"); | ||
//设置请求超时时间 | ||
conn.setConnectTimeout(5000); | ||
code=conn.getResponseCode(); | ||
System.out.println(code); | ||
//200表示获取请求成功 | ||
if(code==200){ | ||
System.out.println("获取数据成功"); | ||
InputStream is=conn.getInputStream(); | ||
String result = readMyInputStream(is); | ||
|
||
//handle Message | ||
Message msg=new Message(); | ||
msg.obj= result; | ||
msg.what=SUCCESS; | ||
handler.sendMessage(msg); | ||
} else { | ||
Message msg = new Message(); | ||
msg.what = ERRORCODE; | ||
handler.sendMessage(msg); | ||
} | ||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
System.out.println("获取数据失败"); | ||
e.printStackTrace(); | ||
Message msg = new Message(); | ||
msg.what = FAILURE; | ||
handler.sendMessage(msg); | ||
} | ||
|
||
|
||
super.run(); | ||
} | ||
}.start(); | ||
} | ||
|
||
//以流的形式读取数据 | ||
public String readMyInputStream(InputStream is){ | ||
byte[] result; | ||
try { | ||
ByteArrayOutputStream baos=new ByteArrayOutputStream(); | ||
byte[] buffer=new byte[1024]; | ||
int len; | ||
while((len = is.read(buffer))!= -1){ | ||
baos.write(buffer,0,len); | ||
} | ||
is.close(); | ||
baos.close(); | ||
result =baos.toByteArray(); | ||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
String errorString="获取数据失败"; | ||
return errorString; | ||
} | ||
return new String(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
app/src/main/res/layout/activity_com1314080901239_get_json.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<LinearLayout 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=".Com1314080901239GetJson" | ||
android:orientation="vertical" | ||
> | ||
|
||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:onClick="click" | ||
android:text="获取Json" | ||
/> | ||
<TextView | ||
android:id="@+id/tv" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/hello_world" /> | ||
|
||
</LinearLayout> |