본문 바로가기
AndroidStudio

웹서비스(WebService) : http통신

by EUN-JI 2023. 8. 30.
<uses-permission android:name="android.permission.INTERNET"/>

manifests  >> 인터넷 허가받고

android:usesCleartextTraffic="true"

 

<application>  http인 url  </application>   안전성확인 : true  //https면 안해도됨.  //http 는 보안이 약함

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:padding="16dp"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load text file"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:padding="8dp"/>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="load image file"/>

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</LinearLayout>

xml 만들기

package com.eunji0118.ex63webservice;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    TextView tv;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn).setOnClickListener(view -> clickBtn());
        findViewById(R.id.btn2).setOnClickListener(view -> clickBtn2());
        tv=findViewById(R.id.tv);
        iv=findViewById(R.id.iv);
    }

    void clickBtn(){
        Thread t=new Thread(){
            @Override
            public void run() {

                //text file의 서버 URL주소
                String address="http://lej0118.dothome.co.kr/index.html";
                address="http://lej0118.dothome.co.kr/aaa.txt";

                try {
                    URL url=new URL(address);
                    InputStream is=url.openStream();
                    InputStreamReader isr=new InputStreamReader(is);  //리더 글자
                    BufferedReader reader=new BufferedReader(isr); //보조스트림 한줄


                    StringBuffer buffer=new StringBuffer();
                    while (true){
                        String line=reader.readLine(); //한줄 가져옴
                        if (line==null) break;
                        buffer.append(line+"\n");  //덮어씀. 한줄만 읽어서 줄바꿈
                    }
                    //쓰레드 위에서 작업
                    runOnUiThread(()->{
                        tv.setText(buffer.toString());
                    });

                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }


            }
        };
        t.start();
    }

    void clickBtn2(){  //glide로 가져온 후
        String address="http://lej0118.dothome.co.kr/paris.jpg";
        Glide.with(this).load(address).into(iv);

//
//        new Thread(){
//            @Override
//            public void run() {
//
//                String address="http://lej0118.dothome.co.kr/paris.jpg";
//
//                try {
//                    URL url=new URL(address);
//                    InputStream is =url.openStream();//문자단위로 읽음
//                    Bitmap bm= BitmapFactory.decodeStream(is);    //그림가져오기
//                    runOnUiThread(()->iv.setImageBitmap(bm));   //run 람다식
//
//
//                } catch (MalformedURLException e) {
//                    throw new RuntimeException(e);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//
//            }
//        }.start();
    }


}

java구현해서 (css01, js01, html01)을 서버를 이용해서 핸드폰(앱)으로 보이기

결과물