Skip to content

Commit

Permalink
Merge pull request #923 from qingtian1993/master
Browse files Browse the repository at this point in the history
#93 #43 实验7
  • Loading branch information
zengsn authored Jun 14, 2016
2 parents 63bd1bd + c49da20 commit 783f323
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.drawingboard"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.drawingboard.Com1314080901225Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.drawingboard.Com1314080901225GetJsonActivity"
android:label="@string/title_activity_com1314080901225_get_json" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void red(View v) {
paint.setColor(Color.RED);
Toast.makeText(getApplicationContext(), "你选择了红色", 0).show();
}

// 黑色画笔
public void black(View v) {
paint.setColor(Color.BLACK);
Expand All @@ -102,6 +102,11 @@ public void brush(View v) {
Toast.makeText(getApplicationContext(), "画笔变粗了", 0).show();
}

public void json(View v){
Intent intent=new Intent(this,Com1314080901225GetJsonActivity.class);
startActivity(intent);
}

//保存图片到sd卡
public void save(View v){
File file=new File("sdcard/zuohua1.png");
Expand All @@ -116,7 +121,7 @@ public void save(View v){
e.printStackTrace();
}

//发送一个就绪广播
//发送一个就绪广播更新图库
Intent intent=new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.example.drawingboard;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;


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 Com1314080901225GetJsonActivity 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(Com1314080901225GetJsonActivity.this, "获取数据成功", Toast.LENGTH_SHORT)
.show();
break;
case FAILURE:
Toast.makeText(Com1314080901225GetJsonActivity.this, "获取数据失败", Toast.LENGTH_SHORT)
.show();
case ERRORCODE:
Toast.makeText(Com1314080901225GetJsonActivity.this, "获取的CODE码不为200!",
Toast.LENGTH_SHORT).show();
default:
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1314080901225_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数据
JSONObject jsonObject = jObject.optJSONObject("weatherinfo");
String city=jsonObject.optString("city");
String city_en=jsonObject.optString("city_en");
String date_y=jsonObject.optString("date_y");
String week=jsonObject.optString("week");
int cityid=jsonObject.optInt("cityid");
String temp1=jsonObject.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="http://192.168.0.143:8080/city.html";
String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901225/city.html";
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);
}

}

0 comments on commit 783f323

Please sign in to comment.