OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), "EUC-KR"));
writer.write(text);
받을때도 아래처럼...
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "EUC-KR")));
String text = reader.readLine();
일반적으로 웹의 글을 가져올때는 테스트로 작성한 코드처럼 httpPost로 글을 가져오지 않고,
isStream으로 아래처럼 가져오므로, 이 때는 한글 변환에 문제가 없으리라 생각된다.
url에서 stream을 가져온후, post 방식으로 redirected된 url을 가져오던 소스의 bufferRead하던 부분과
조합해서 위의 인코딩을 잘 넣어보면 한글도 읽혀지지 않을까 싶다.
아래에서 싸이주소값을 가져올때는 한글이 깨졌었다.
/**
* url에서 이미지를 가져와서 sdcard에 저장하는 함수
* fullPath=urlPrefix + fileName
*
* @author wono77(2010.04.11)
* @param urlPrefix : "http://radstyle.net/images/cp/"
* @param fileName : a.jpg
*/
public void imageSaveFromURL(String urlPrefix, String fileName) {
String fullImageUrl = urlPrefix + fileName;
// String sdcardPath =
// Environment.getExternalStorageDirectory().toString();//+ "/sdcard/";
String sdcardFullPath = sdcardPath + fileName;
// url을 파일에 저장
InputStream is = null;
try {
is = getUrlStream(fullImageUrl);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// directory 존재여부 체크 한번
File dir = new File(sdcardPath);
if (!dir.exists()) {
dir.mkdir();
}
if (is != null) {
try {
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(new File(sdcardFullPath));
int n = 0;
try {
while ((n = bis.read()) != -1) {
fos.write(n);
}
} catch (IOException e) {
Log.v("a", "write fail");
} finally {
fos.close();
bis.close();
}
} catch (IOException e) {
Log.v("a", "fail");
}
}
}
/**
* url에서 inputStream을 받아오는 함수
*
* @author wono77(2010.04.11)
* @param path
* @return
* @throws Exception
*/
public InputStream getUrlStream(String path) throws Exception {
InputStream is = null;
try {
// 20100222 최원효 : timeout 셋팅
int desiredValue = 3000;
URL sourceImageUrl = new URL(path);
URLConnection t_connection = sourceImageUrl.openConnection();
t_connection.setConnectTimeout(desiredValue);
t_connection.setReadTimeout(desiredValue);
is = t_connection.getInputStream();
} catch (Exception e) {
if (is != null) {
is.close();
}
is = null;
}
return is;
}
/**
* url을 입력받아 direct 되는 url의 내용을 가져오는 함수
*
* @author wono77(2010.08.24)
* @param inputUrl
* @return
* @throws IOException
*/
public String getRedirectedUrlStream(String inputUrl) {
URL url = null;
String inputLine = "";
String response = "";
URLConnection conn = null;
try {
url = new URL(inputUrl);
conn = url.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) conn;
try {
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-type", "text/xml");
httpConn.setUseCaches(false);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
String testxml = null;
DataOutputStream out;
out = new DataOutputStream(httpConn.getOutputStream());
out.flush();
out.close();
// This next line throws an IOException if response code is not
// 200 <<======<<
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
response = response + inputLine;
}
in.close();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
} else {
return null;
}
}
[설명]
이번장에서는 웹과의 데이타 교환에 대해 연속적으로 다루어 보겠습니다.
그 첫번째로 post/get 방식으로 웹에 데이타를 전달하고 결과치를 다시 받는 것을 처리하겠습니다.
일단 어플에서 불러올 php 파일을 작성하겠습니다.
이 php파일은 변수를 받아서 출력해주는 프로그램입니다.
android_post.php
<?php
$id = $_POST["user_id"] ;
$pwd = $_POST["user_pwd"] ;
echo "your id is ".$id." and your passwd is ".$pwd;
?>
[설명]
이제는 AndroidManifest.xml에서 인터넷에 대한 권한이 줍니다.
<uses-permission android:name="android.permission.INTERNET" />
public class ProjectLinkageWeb extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);