Skip to content

Commit

Permalink
#6 第六次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengzid committed Jun 2, 2018
1 parent 24cb088 commit 6ace2cd
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
10 changes: 8 additions & 2 deletions com1614080901143/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.com1614080901143">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -17,7 +22,8 @@
</intent-filter>
</activity>
<activity android:name=".com_1614080901143Activity" />
<activity android:name=".ThirdActivity"></activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".jsonActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hzuapps.androidlabs.com1614080901143;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand All @@ -21,6 +22,8 @@ public void onClick(View view) {
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("便签内容",editText.getText().toString());
editor.apply();
Intent intent=new Intent(ThirdActivity.this,jsonActivity.class);
startActivity(intent);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package edu.hzuapps.androidlabs.com1614080901143;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class jsonActivity extends AppCompatActivity {
private String text;
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
listView=(ListView)findViewById(R.id.listview);
getjson();
}
public void getjson()
{
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://raw.githubusercontent.com/1614080901143/android-labs-2018/master/com1614080901143/content.json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setUseCaches(false);
connection.connect();
InputStream in = connection.getInputStream();
InputStreamReader input=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(input);
if (connection.getResponseCode() == 200) {
StringBuilder response = new StringBuilder();
String Line;
while ((Line = reader.readLine()) != null) {
response.append(Line);
}
text = response.toString();
Log.v("out---------------->",text);
analyzejson();
}
}
catch(Exception e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override public void run() {
adapter=new ArrayAdapter<String>(jsonActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
}
}).start();
}
public void analyzejson()
{
try{
JSONArray jsonArray=new JSONArray(text);
for(int a=0;a<jsonArray.length();a++)
{
JSONObject jsonObject=jsonArray.getJSONObject(a);
String number=jsonObject.getString("number");
String number1=jsonObject.getString("number1");
String number2=jsonObject.getString("number2");
String add;
add=number+" "+number1+" "+number2;
list.add(add);
Log.v("结果",add);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Binary file not shown.
15 changes: 15 additions & 0 deletions com1614080901143/app/src/main/res/layout/activity_json.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.com1614080901143.jsonActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>

</android.support.constraint.ConstraintLayout>

0 comments on commit 6ace2cd

Please sign in to comment.