0.AndroidManifest.xml

//internet permission

//CleartextTraffic true설정

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpproject">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

 

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

<EditText
android:id="@+id/register_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PW"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

<EditText
android:id="@+id/register_pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal">

<Button
android:id="@+id/register_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="회원가입" />
</TableRow>
</TableLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

//url ip값으로 주기

package com.example.httpproject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

Button registerBtn;
EditText idet, pwet;

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

setTitle("ORACLE");

registerBtn = (Button)findViewById(R.id.register_btn);
idet = (EditText)findViewById(R.id.register_id);
pwet = (EditText)findViewById(R.id.register_pw);

registerBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try {
String result;
String id = idet.getText().toString();
String pwd = pwet.getText().toString();

RegisterActivity task = new RegisterActivity();
result = task.execute(id, pwd).get();
} catch (Exception e) {
Log.i("DBtest", ".....ERROR.....!");
}
}
});

}
}

 

RegisterActivity.java

package com.example.httpproject;

import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RegisterActivity extends AsyncTask<String, Void, String> {
String sendMsg, receiveMsg;

@Override
protected String doInBackground(String... strings) {
try {
String str;

// 접속할 서버 주소 (이클립스에서 android.jsp 실행시 웹브라우저 주소)
URL url = new URL("http://10.241.131.162:8080/android/data.jsp");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());

// 전송할 데이터. GET 방식으로 작성
sendMsg = "id=" + strings[0] + "&pwd=" + strings[1];

osw.write(sendMsg);
osw.flush();

//jsp와 통신 성공 시 수행
if (conn.getResponseCode() == conn.HTTP_OK) {
InputStreamReader tmp = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(tmp);
StringBuffer buffer = new StringBuffer();

// jsp에서 보낸 값을 받는 부분
while ((str = reader.readLine()) != null) {
buffer.append(str);
}
receiveMsg = buffer.toString();
} else {
// 통신 실패
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//jsp로부터 받은 리턴 값
return receiveMsg;
}

}

'android studio' 카테고리의 다른 글

android - jsp - database connection  (0) 2019.09.08
android internet check  (0) 2019.09.05
activity life cycle  (0) 2019.08.14
intent  (0) 2019.08.14
startActivityForResult()(화면전환)  (0) 2019.08.13

+ Recent posts