530
No
class MainActivity
package kr.co.braverokmc.myhttp;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//웹으로 요청하기
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
}
public void onClick(View v){
RequestThread thread=new RequestThread();
thread.setDaemon(true);
thread.start();
}
class RequestThread extends Thread{
@Override
public void run() {
try{
String urlStr =editText.getText().toString();
StringBuilder outputBuilder =new StringBuilder();
URL url =new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//기본 속성 설정
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(15000); //접속시간 15초 여유
int resCode =connection.getResponseCode();
if(resCode==HttpURLConnection.HTTP_OK){
BufferedReader reader =new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line=null;
while (true){
line=reader.readLine(); //한줄 읽기
if(line==null){
break;
}
outputBuilder.append(line+"\n");
}
reader.close();
connection.disconnect();
println(outputBuilder.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
private void println(final String data){
handler.post(new Runnable() {
@Override
public void run() {
textView.append(data +"\n");
}
});
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.myhttp.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="https://m.naver.com/"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/editText"
/>
<Button
android:text="Http 요청하기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText"
android:id="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onClick"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="@+id/editText"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView"
/>
</ScrollView>
</RelativeLayout>
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET" />
98강
99강
댓글 ( 4)
댓글 남기기