get방식으로 서버에 요청


안드로이드 버전을 구버전으로 낮춰서 작업함. (API 19)



1. 레이아웃


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.progdv.app2_network.MainActivity">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/userId"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="아이디" />

<EditText
android:id="@+id/pwd"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="비밀번호" />

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="로그인"
/>

</LinearLayout>

<TextView
android:id="@+id/result"
android:text="로그인 응답 결과 출력"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>







2. 메인 액티비티


package com.progdv.app2_network;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends Activity {
EditText userId, pwd;
Button login;
TextView result;

String tmpResult;

Handler handler = new Handler();

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

userId = (EditText)findViewById(R.id.userId);
pwd = (EditText)findViewById(R.id.pwd);
login = (Button)findViewById(R.id.login);
result = (TextView)findViewById(R.id.result);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final MyDialogFragment fragment = MyDialogFragment.newInstance();
fragment.show(getFragmentManager(), "TAG");

String id = userId.getText().toString();
String password = pwd.getText().toString();

ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair("userid", id));
list.add(new BasicNameValuePair("passwd", password));

final String urlPath = "http://211.183.9.67:8095/loginTest/login.jsp?"
+ URLEncodedUtils.format(list, "UTF-8");

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
InputStream is = requestGet(urlPath);
tmpResult = streamToString(is);

handler.post(new Runnable() {
@Override
public void run() {
result.setText(tmpResult);
fragment.dismiss();
}
});
}
});

thread.start();
}
});
}

public InputStream requestGet(String urlPath){

try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlPath);
HttpResponse response = client.execute(request);

HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return is;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public static class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance(){
return new MyDialogFragment();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setTitle("네트워크");
dialog.setMessage("요청중...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

return dialog;
}
}

private String streamToString(InputStream is){
StringBuffer buffer = new StringBuffer();


try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String str = reader.readLine();
while (str != null){
buffer.append(str);
str = reader.readLine();
}

reader.close();
} catch (IOException e) {
e.printStackTrace();
}

return buffer.toString();
}
}




3. 서버 구축


이클립스에서 다이나믹 웹 프로젝트 생성, 위에서 설정한대로 login.jsp라는 파일을 생성한다.







<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>안드로이드 연동 테스트</title>

</head>

<body>


<%

String id = request.getParameter("userId");

String pwd = request.getParameter("passwd");

String msg = "아이디 : " + id + ", 비밀번호 : " + pwd;

%>


</body>

</html>







Posted by netyhobby
,