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
xiwu zhong
committed
Jun 16, 2017
1 parent
d21cebf
commit 4343316
Showing
4 changed files
with
107 additions
and
29 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
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
90 changes: 90 additions & 0 deletions
90
...java/edu/hzuapps/androidlabs/homeworks/net1414080903136/Net1414080903136MainActivity.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,90 @@ | ||
package edu.hzuapps.androidlabs.homeworksrks.net1414080903136; | ||
|
||
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 Net1414080903136ShowJsonActivity 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_net1414080903136_show_json); | ||
tv= (TextView) findViewById(R.id.tv_json); | ||
new Thread(){ | ||
@Override | ||
public void run() { | ||
super.run(); | ||
String a=a("https://raw.githubusercontent.com/zhongxiwu/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903136/1414080903136.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("id")); | ||
sb.append("\n\n"); | ||
|
||
sb.append(object.getString("state")); | ||
} | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
AndroidLabs/app/src/main/res/layout/activity_net1414080903136_main.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" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_net1414080903136_main" | ||
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.homworks.net1414080903136.Net1414080903136MainActivity"> | ||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/tv_json"/> | ||
</RelativeLayout> |