This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1140 from 522090231/master
- Loading branch information
Showing
2 changed files
with
110 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,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; | ||
} | ||
} |
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,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> |