package com.cookandroid.jsp01;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button btn;
EditText etName;
EditText etAddress;
EditText et_webpage_src;
DownloadWebpageTask myTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button_call);
btn.setOnClickListener(this);
etName=(EditText)findViewById(R.id.name);
etAddress=(EditText)findViewById(R.id.address);
et_webpage_src = (EditText)findViewById(R.id.webpage_src);
}
public static String encodeString(Properties params) {
StringBuffer sb = new StringBuffer(256);
Enumeration names = params.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = params.getProperty(name);
sb.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value) );
if (names.hasMoreElements()) sb.append("&");
}
return sb.toString();
}
private class DownloadWebpageTask extends AsyncTask<String,Void,String> {
//주요 내용 실행
@Override
protected String doInBackground(String... urls) {
try {
return (String) downloadUrl((String) urls[0]);
} catch (IOException e) {
return "다운로드 실패";
}
}
private String downloadUrl(String myurl) throws IOException {
HttpURLConnection urlConnection = null;
URL url=null;
try {
Properties prop = new Properties();
prop.setProperty("name", etName.getText().toString());
prop.setProperty("address", etAddress.getText().toString());
String encodedString = encodeString(prop);
/* Get방식으로 처리 시작 ==========================*/
// url = new URL(myurl+ "?" + encodedString);
// urlConnection = (HttpURLConnection) url.openConnection();
/* Get방식으로 처리 끝 */
/* post방식으로 처리 시작 ==========================*/
url = new URL(myurl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
DataOutputStream out = null;//
out = new DataOutputStream(urlConnection.getOutputStream());//
out.writeBytes(encodedString);
out.flush();
/* post방식으로 처리 끝 ===============================*/
BufferedInputStream buf = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufreader = new BufferedReader(new InputStreamReader(buf, "utf-8"));
String line = null;
String page = "";
while ((line = bufreader.readLine()) != null) {
page += line;
}
return page;
} finally {
urlConnection.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
try {
// 읽어들인 JSON 포맷의 데이어틀 JSON 객체로 변환
JSONObject json = new JSONObject(result);
// customers에 해당하는 배열을 할당
JSONArray jArr = json.getJSONArray("customers");
// 배열의 크기만큼 반복하면서, name과 address의 값을 추출함
for (int i = 0; i < jArr.length(); i++) {
// i번째 배열 할당
json = jArr.getJSONObject(i);
// name과 address의 값을 추출함
String name = json.getString("name");
String address = json.getString("address");
// name과 address의 값을 출력함
et_webpage_src.append(name + "\n");
et_webpage_src.append(address + "\n");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
// @Override
public void onClick(View v) {
String url="http://10.0.2.2:8181/JSON01/test.jsp";
//AsyncTask실행
myTask=new DownloadWebpageTask();
myTask.execute(url);
}
}