Skip to content

Commit

Permalink
hzuapps#6 hzuapps#405 第六次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
1814080911138 committed Jan 4, 2021
1 parent 2f0fee5 commit 228e9d1
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
107 changes: 107 additions & 0 deletions students/sec1814080911138/HttpClientActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package edu.hzuapp.androidlabs.sec1814080911138;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLDecoder;

public class HttpClientActivity extends AppCompatActivity {
TextView textView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_client);

((Button) findViewById(R.id.button_get_issues)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
textView = findViewById(R.id.text1);

String jsonText = getGitHubIssues();
if (jsonText != null) {
try {
JSONArray jsonArr = new JSONArray(jsonText);
JSONObject jsonObj = (JSONObject) jsonArr.get(1);
int number = jsonObj.getInt("number");
final int num = number;
System.out.println("NUMBER = " + number);
textView.post(new Runnable() {
@Override
public void run() {
textView.setText("NUMBER = " + num+"\n"+"详细打印情况请看控制台");
}
});
//((EditText) thisActivity.findViewById(R.id.first_node_number)).setText(Integer.toString(number));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
});


}

private String getGitHubIssues() {
String gitApi = "https://api.github.com/repos/hzuapps/android-labs-2020/issues";
//gitApi = "https://www.baidu.com";
URL url = null;
String jsonText = null;
try {
url = new URL(gitApi);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// HttpClient
// OKHttp
//connection.setDoOutput(true);
//connection.setDoInput(true);
//connection.setRequestMethod("POST");
//connection.setUseCaches(false);
//connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = URLDecoder.decode(lines, "utf-8");
sb.append(lines);
}
System.out.println(sb);
jsonText = sb.toString();
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonText;
}
}
27 changes: 27 additions & 0 deletions students/sec1814080911138/activity_http_client.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".HttpClientActivity">

<Button
android:id="@+id/button_get_issues"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="29dp"
tools:layout_editor_absoluteY="27dp" />

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="@id/button_get_issues"
android:textSize="30dp"
/>


</RelativeLayout>

0 comments on commit 228e9d1

Please sign in to comment.