반응형
화면 구성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?xml version="1.0" encoding="utf-8"?>
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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input name"
android:inputType="text"/>
<EditText
android:id="@+id/et_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input message"
android:inputType="textMultiLine"
android:maxLines="5"/>
<!-- 처음 시작은 한 줄이고 5줄까지만 커지고, 그 다음줄부터 스크롤링됨.-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GET METHOD"
android:onClick="clickGet"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GET POST"
android:onClick="clickPost"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="24sp"
android:padding="8dp"/>
</LinearLayout>
|
라이브러리 추가하자.
퍼미션 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?xml version="1.0" encoding="utf-8"?>
<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/AppTheme"
android:usesCleartextTraffic="true">
<!-- android:usesCleartextTraffic="true" ore버전 부터 http 사이트 접근 금지되서 이를 해결-->
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
<!-- oreo중에 일부기기에 ERROR-->
<!-- volley라이브러리가 http통신 기술을 사용하기에..oreo 운영체제에서 동작을 막아버림-->
android:required="false"/>
</application>
</manifest>
|
MainActivity.java 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
EditText etName, etMsg;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName=findViewById(R.id.et_name);
etMsg=findViewById(R.id.et_msg);
tv=findViewById(R.id.tv);
}
public void clickGet(View view) {
String name= etName.getText().toString();
String msg= etMsg.getText().toString();
//Get 방식으로 보낼 서버 주소
//Get방식을 통해 보낼 데이터를 URL 뒤에 붙이기 위해 utf-8로 인코딩(한글은 불가해서)
try {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String getUrl=serverUrl+"?name="+name+"&msg="+msg;
//이때 서버로 부터 돌려받은 결과 Data가 String일때 사용하는 요청객체 생성
StringRequest stringRequest= new StringRequest(Request.Method.GET, getUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//while문으로 line해서 읽어오는 작업과 UI에 업데이트하는 runOnUiThread도 안만들어도 된다.
tv.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
}
});
//서버와 데이터를 주고 받는 요청 객체를
//서버로 보내줄 우체통 같은 역할의 객체
//요청큐(RequestQueue)
RequestQueue requestQueue= Volley.newRequestQueue(this);
//우체통에 요청 편지 넣기
//요청큐에 요청 객체 추가..
requestQueue.add(stringRequest);
}
public void clickPost(View view) {
final String name= etName.getText().toString();
final String msg= etMsg.getText().toString();
//Post 방식으로 보낼 서버 주소
//결과를 String으로 받는 객체
StringRequest stringRequest= new StringRequest(Request.Method.POST, serverUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tv.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
}
}){
//POST 방식으로 보낼 데이터를
//리턴해주는 콜백 메소드
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> datas= new HashMap<>();
datas.put("name", name);
datas.put("msg",msg);
return datas;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
|
<실행 화면>
반응형
'안드로이드 웹앱 콘테츠 개발자 양성(국비지원) > HTML 도메인' 카테고리의 다른 글
HTTP 서버와 DB 앱 연동 (텍스트,이미지를 GET,POST로 DB까지) 1-2 (7) | 2019.10.22 |
---|---|
HTTP 서버와 DB 앱 연동 (텍스트,이미지를 GET,POST로 DB까지) 1-1 (13) | 2019.10.21 |
HTTP 서버와 앱 연동 (0) | 2019.10.21 |
HTML(언어 사용 html,css,js, php) 기존 도메인을 이용 GET,POST 방식으로 서버 업로드 1-2 [DB 연동] (1) | 2019.10.18 |
HTML(언어 사용 html,css,js, php) 기존 도메인을 이용 GET,POST 방식으로 서버 업로드 1-1 (0) | 2019.10.18 |
댓글