forked from hzuapps/android-labs-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87a6a54
commit f7c1130
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
.../edu/hzuapps/androidlabs/homeworks/net1414080903113/Net1414080903113ShowJsonActivity.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,96 @@ | ||
package edu.androidlabs.homeworks.Net1414080903113; | ||
|
||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.TextView; | ||
|
||
import com.example.administrator.studentjob.R; | ||
|
||
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; | ||
|
||
public class Net1414080903113ShowJsonActivity 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_net1414080903113_show_json); | ||
tv= (TextView) findViewById(R.id.tv_json); | ||
new Thread(){ | ||
@Override | ||
public void run() { | ||
super.run(); | ||
String a=a("https://raw.githubusercontent.com/zhouchenghai/android-labs-2017" + | ||
"/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks" + | ||
"/net1414080903113/1414080903113.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("location")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("salary")); | ||
sb.append("\n\n"); | ||
sb.append(object.getString("occurTime")); | ||
} | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
AndroidLabs/app/src/main/res/layout/activity_net1414080903113_show_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,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/activity_show_json" | ||
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"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/tv_json"/> | ||
|
||
</RelativeLayout> |