AsyncTask λ λΉλκΈ° μμ μ ν μ μλλ‘ μ 곡νλ ν΄λμ€μ΄λ€.
λΉλκΈ°λ₯Ό μ¬μ©νλ μ΄μ λ,
λκΈ°μμΌλ‘ μμ μ νκ² λλ©΄ main thread μμ A thread λ‘ κ°μ μμ μ νλ€κ°.. A thread κ° λλμΌλ§ λ€μ main thread λ‘ λμ μ¬ μ μλ€. λ§μ½ A thread κ° μμ μ λλ΄μ§ λͺ» νλ©΄,,, main thread λ λ€μ μμ μ ν μ μλ κ²μ΄λ€.
λΉλκΈ°μμΌλ‘ μμ μ νκ² λλ©΄ main thread λ main thread λλ‘.. A thread λ A thread λλ‘... μμ μ μ§νν μ μκ² λλ€.
HttpURLConnection μ μ΄μ©ν΄ jsp μλ²λ‘ λ°μ΄ν°λ₯Ό 보λ΄κ³ , jsp μλ²λ‘λΆν° λ°μ΄ν°λ₯Ό λ°λ κ°λ¨ν μνμ λ§λ€μ΄ 보μ.
λ°μ΄ν°λ JSONObject λ₯Ό 보λ΄κ³ λ°λ κ±Έλ‘ μλ₯Ό λ€μ΄ λ³΄κ² λ€.
- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devshin93.asynctasktest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <!-- μΆκ° -->
<uses-permission android:name="android.permission.INTERNET"/> <!-- μΆκ° -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AsyncTaskTest"
android:usesCleartextTraffic="true"> <!-- μΆκ° -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- res > layout > activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#4F599F">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="AsyncTask λ‘ HttpURLConnection μ¬μ©νκΈ°"
android:textSize="18dp"
android:textColor="@color/white"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:orientation="vertical"
android:background="@color/white">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:background="#4F599F">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:text="보λ΄λ κ°" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:orientation="vertical"
android:background="@color/white">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="input 1 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/sendText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="input 2 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/sendText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/async_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="μλ²λ‘λΆν° κ²°κ³Ό λ°μ μ€κΈ°" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:background="#4F599F">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:text="λ°μ κ°" />
</LinearLayout>
<TextView
android:id="@+id/getText"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/grey"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
- res > values > colors.xml μ grey μμμ νλ μΆκ°ν΄ 쀬λ€..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="grey">#C0C0C0</color>
</resources>
- java > ν¨ν€μ§λͺ > MainActivity.java
package com.devshin93.asynctasktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.async_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// AsyncTask κ°μ²΄ μμ±
HttpUrlConnectionTest test = new HttpUrlConnectionTest();
// μμ
test.execute();
}
});
}
// AsyncTask<Params, Progress, Result>
// Params : doInBackground μ νλΌλ―Έν° νμ
// Progress : onProgressUpdate μ νλΌλ―Έν° νμ
// Result : doInBackground μ λ¦¬ν΄ κ°. -> onPostExecute μ νλΌλ―Έν° νμ
.
public class HttpUrlConnectionTest extends AsyncTask<String, Void, String> {
EditText ed1 = findViewById(R.id.sendText1);
EditText ed2 = findViewById(R.id.sendText2);
String sInput1 = ed1.getText().toString();
String sInput2 = ed2.getText().toString();
@Override
protected void onPreExecute() {
// doInBackground() μ΄μ μ μνλ μμ
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
String url = "http://10.10.28.97:8090/AndroidAsyncTaskTest/test.jsp";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(10000); // read μ νμμμ μκ°
conn.setConnectTimeout(15000); // μλ² μ μ μ μ°κ²° νμμμ μκ°
conn.setRequestMethod("POST"); // POST λ°©μμΌλ‘ μμ²
conn.setDoInput(true); // InputStream μΌλ‘ μλ²λ‘λΆν° μλ΅μ λ°κ² λ€λ μ΅μ
conn.setDoOutput(true); // OutputStream μΌλ‘ POST λ°μ΄ν°λ₯Ό λ겨 μ£Όκ² λ€λ μ΅μ
conn.setRequestProperty("Content-Type", "application/json"); // application/json νμμΌλ‘ μ μ‘. (Request body μ λ¬μ application/json μΌλ‘ μλ²μ μ λ¬)
conn.setRequestProperty("Cache-Control", "no-cache");
// JSONObject λ‘ λ°μ΄ν°λ₯Ό λ¬Άμλ€.
JSONObject jsonObj = new JSONObject();
jsonObj.put("input1", sInput1);
jsonObj.put("input2", sInput2);
// JSONObject λ‘ λ¬Άμ λ°μ΄ν°λ₯Ό toString().getBytes() ν΄ μ μ‘
OutputStream os = conn.getOutputStream();
os.write(jsonObj.toString().getBytes());
os.close();
// ν΅μ ν μλ²λ‘λΆν° κ°μ Έ μ¨ μλ΅μ½λ. 200 -> μ±κ³΅
int retCode = conn.getResponseCode();
if (retCode == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = br.readLine()) != null) {
response.append(line);
response.append("");
}
br.close();
// μλ²μμ λ³΄λΈ κ°μ΄ JSONObject νμμ΄λκΉ..
String sRet = response.toString();
JSONObject retJSONObj = new JSONObject(sRet);
if (retJSONObj.get("nRv").equals("0")) {
String retVal = (String)retJSONObj.get("sRet");
System.out.println("λ©λ‘±"+retVal);
return retVal;
}
else {
System.out.println("νλ ·μ΄");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
// doInBackground() μ΄νμ μνλ μμ
// String s νλΌλ―Έν°λ doInBackground() μ 리ν΄κ°μ΄λ€.
TextView tv = findViewById(R.id.getText);
tv.setText(s);
}
}
}
μλ² μν μ½λλ λ€μ μ₯μ μ΄μ΄ μμ±νκ² λ€...
'π κ°λ° > μλλ‘μ΄λ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μλλ‘μ΄λ] μμΈμ μ°Ύλ μ€... ^^ (2) | 2021.09.14 |
---|---|
[μλλ‘μ΄λ] java.net.SocketException: socket failed: EACCES (Permission denied) (0) | 2021.09.14 |
[μλλ‘μ΄λ] AAR(Android Archive) ν μ€νΈ ... 2 (2) | 2021.08.12 |
[μλλ‘μ΄λ] AAR(Android Archive) λ§λ€κΈ° ... 1 (0) | 2021.08.12 |
[μλλ‘μ΄λ] Java to Kotlin, Kotlin to Java (0) | 2021.07.30 |
λκΈ