Android > HttpURLConnection - Á¢¼Ó¹æ¹ý
µî·ÏÀÏ : 2017-07-06 17:32
Á¶È¸¼ö : 52,967
ÀÚ¹Ù¿¡¼ HTTP Ŭ¶óÀ̾ðÆ®¸¦ ¸¸µå´Â °¡Àå °£´ÜÇÑ ¹æ¹ýÀº URL °´Ã¼¸¦ ¸¸µé°í ÀÌ °´Ã¼ÀÇ openConnection() ¸Þ¼Òµå¸¦ È£ÃâÇÏ¿©
HttpURLConnection °´Ã¼¸¦ ¸¸µå´Â °ÍÀÔ´Ï´Ù.
HttpURLConnection °´Ã¼¸¦ ÀÌ¿ëÇØ À¥¿¡ Á¢±ÙÈÄ °á°ú °ª¿¡ µû¶ó¼ ÇÁ·Î±×·¡¹Ö ó¸®ÇÏ´Â ±â¹ýÀ» ³ªÅ¸³À´Ï´Ù.
try {
//ÈÞ´ëÆù¹øÈ£ ¹Þ±â
String _PhoneNo = ""; //ÈÞ´ëÆù¹øÈ£
TelephonyManager telephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
_PhoneNo = telephony.getLine1Number();
if(_PhoneNo==null)
{
_PhoneNo = "";
}
url = new URL("http://entersoft.kr/member/member_login_ok.asp?phoneno="+_PhoneNo);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuffer buffer = new StringBuffer();
int c;
while((c=in.read()) != -1)
{
buffer.append((char)c);
}
_Url_ReturnChk = buffer.toString();
if(_Url_ReturnChk.equals("OK"))
{
Toast.makeText(getApplicationContext(),"·Î±×ÀÎÀÌ ¼º°øÇÏ¿´½À´Ï´Ù.",500).show();
Member_Login.this.finish();
Intent myIntent1 = new Intent(getApplicationContext(), Main_Menu.class);
startActivity(myIntent1);
}
else
{
Toast.makeText(getApplicationContext(),"·Î±×ÀÎÀÌ ½ÇÆÐÇÏ¿´½À´Ï´Ù.",500).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
¾Æ·¡ ¼Ò½º´Â ¿¹¿Ü »óȲ±îÁö °í·ÁÇÑ ±âº» »ùÇà ¿¹Á¦ ÀÔ´Ï´Ù.
¸®ÅÏµÈ ¹®ÀÚ¿À» ¹Þ¾Æ txtMsg ¿¡ »Ñ·ÁÁÖ´Â ¿¹Á¦ ÀÔ´Ï´Ù.
String urlStr = "http://m.naver.com";
String output = request(urlStr); //request() ¸Þ¼Òµå È£Ãâ
txtMsg.setText(output); //°á°ú¹° Ãâ·Â
private String request(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn!=null) {
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
int resCode = conn.getResponseCode();
if(resCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while(true) {
line = reader.readLine();
if(line==null) {
break;
}
output.append(line + "\n");
}
reader.close();
conn.disconnect();
}
}
}catch(Exception ex) {
Log.e("SampleHTTP","Exception in processing response.",ex);
}
return output.toString();
}