-
Notifications
You must be signed in to change notification settings - Fork 78
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 #495 from livehan/master
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...s/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1412070501227/1412070501227.json
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 @@ | ||
[{"name":"李维韩","sex":"男","clazz":"14网络1班","dormitory":"5#B503","age":"20"}] |
95 changes: 95 additions & 0 deletions
95
.../edu/hzuapps/androidlabs/homeworks/net1412070501227/Net1412070501227ShowJsonactivity.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,95 @@ | ||
package edu.hzuapps.androidlabs.homeworks.net1412070501227; | ||
|
||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import edu.hzuapps.androidlabs.R; | ||
public class Net1412070501227ShowJsonactivity extends AppCompatActivity { | ||
TextView tv; | ||
|
||
Handler handler=new Handler(){ | ||
@Override | ||
public void handleMessage(Message msg) { | ||
super.handleMessage(msg); | ||
String a= parseJson((String) msg.obj); | ||
tv.setText(a); | ||
} | ||
}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_net1414080903133_show_json); | ||
tv= (TextView) findViewById(R.id.tv_json); | ||
new Thread(){ | ||
@Override | ||
public void run() { | ||
super.run(); | ||
String a=a("https://raw.githubusercontent.com/livehan/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1412070501227/1412070501227.json"); | ||
Message msg=handler.obtainMessage(); | ||
msg.obj=a; | ||
handler.sendMessage(msg); | ||
} | ||
}.start(); | ||
} | ||
|
||
public String a(String u) { | ||
try { | ||
URL url = new URL(u); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setConnectTimeout(5000); | ||
InputStream is = conn.getInputStream(); | ||
byte[] b = new byte[1024]; | ||
StringBuilder sb = new StringBuilder(); | ||
String a; | ||
int len; | ||
while ((len=is.read(b)) != -1) { | ||
a=new String(b,0,len); | ||
sb.append(a); | ||
} | ||
return sb.toString(); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
public String parseJson(String data){ | ||
StringBuilder sb=new StringBuilder(); | ||
try { | ||
JSONArray jsonArray=new JSONArray(data); | ||
for (int i=0;i<jsonArray.length();i++){ | ||
JSONObject object=jsonArray.getJSONObject(i); | ||
sb.append(object.getString("name")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("clazz")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("sex")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("dormitory")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("age")); | ||
} | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
return sb.toString(); | ||
|
||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
AndroidLabs/app/src/main/res/layout/activity_net1412070501227_showjson.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,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_net1412070501227_showjson" | ||
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="edu.hzuapps.androidlabs.homeworks.net1412070501227.Net1412070501227ShowJsonactivity"> | ||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/tv_json"/> | ||
|
||
</RelativeLayout> |