forked from hzuapps/android-labs-2020
-
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
2f0fee5
commit 228e9d1
Showing
2 changed files
with
134 additions
and
0 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
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; | ||
} | ||
} |
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,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> |