Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1140 from 522090231/master
Browse files Browse the repository at this point in the history
#6 #15 第6次实验
  • Loading branch information
zengsn authored Nov 26, 2020
2 parents 5f8aa65 + 0fc7057 commit d09de8c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
93 changes: 93 additions & 0 deletions students/net1814080903207/HttpClientActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package edu.hzuapps.androidlabs.Net1814080903207;

import androidx.appcompat.app.AppCompatActivity;

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

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 {

@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() {
String jsonText = getGitHubIssues();
if (jsonText != null) {
try {
JSONArray jsonArr = new JSONArray(jsonText);
JSONObject jsonObj = (JSONObject) jsonArr.get(1);
int number = jsonObj.getInt("number");
System.out.println("NUMBER = " + number);
//((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;
}
}
17 changes: 17 additions & 0 deletions students/net1814080903207/activity_http_client.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".HttpClientActivity">

<Button
android:id="@+id/button_get_issues"
android:layout_width="406dp"
android:layout_height="301dp"
android:text="Button"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="2dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit d09de8c

Please sign in to comment.