-
Lib 추가
implementation 'com.android.volley:volley:1.1.1'
입력 후 'Sync Now' 클릭
Manifest 설정
<uses-permission android:name="android.permission.INTERNET"/> ... android:usesCleartextTraffic="true"
추가한 코드
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Response.Listener<String> rplsn = new Response.Listener<String>() { @Override public void onResponse(String response) { ((TextView)findViewById(R.id.textview)).setText(response.toString()); if(false) try { JSONObject jo = new JSONObject(response); Toast.makeText(getApplicationContext(), "Dbg onResponse: " + jo.toString(), Toast.LENGTH_SHORT).show(); } catch (JSONException e) { Toast.makeText(getApplicationContext(), "Err onResponseJson: " + e.toString(), Toast.LENGTH_LONG).show(); } } }; Response.ErrorListener errlsn = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "Err ErrorListener: " + error.toString(), Toast.LENGTH_SHORT).show(); } }; String url = "http://google.com"; StringRequest req = new StringRequest(Request.Method.GET, url, rplsn, errlsn); //req.setShouldCache(false); RequestQueue rq = Volley.newRequestQueue(MainActivity.this); //rq.getCache().clear(); rq.add(req); } }
실행 결과
다운로드
'Android' 카테고리의 다른 글
AndroidStudio - Auto Import (0) 2020.07.18 Android - AndroidManifest.xml (0) 2020.07.16 Android - gradle 파일 (0) 2020.07.16 Android 용어 (0) 2020.07.16 AndroidStudio - Youtube 재생기 만들기(YouTube Android Player API) (0) 2020.07.12 AndroidStudio - 화면 전환 (Activity 전환) (0) 2020.07.12 AndroidStudio - 종료 Button 추가하기 (0) 2020.07.12 AndroidStudio - 단순 계산기 앱(Apk) 만들기 (0) 2020.07.11