Webserver를 통해 가져오는 방식 입니다.
package aa.testapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainTest extends Activity {
EditText edtTxt ;
TextView resultTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtTxt = (EditText)findViewById(R.id.edtTxt);
resultTxt = (TextView)findViewById(R.id.resulttxt);
Button btnOk = (Button)findViewById(R.id.btnOK);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HttpPostData();
}
});
}
//------------------------------
// Http Post
//------------------------------
public void HttpPostData() {
try {
//--------------------------
// URL
//--------------------------
URL url = new URL("http://xxx.xxx.xxx.xxx/mobile/login.asp");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setDefaultUseCaches(false);
http.setDoInput(true);
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
StringBuffer buffer = new StringBuffer();
buffer.append("name").append("=").append(edtTxt.getText().toString()); // 넘길 변수
OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "EUC-KR");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "EUC-KR");
BufferedReader reader = new BufferedReader(tmp);
StringBuilder builder = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
builder.append(str + "\n");
}
resultTxt.setText(builder.toString());
} catch (MalformedURLException e) {
// err msg
} catch (IOException e) {
// err msg
} // try
} // HttpPostData
}
넘겨온 방식을 XML로 하시고 XML parsing 하시면 됩니다.
다음엔 소켓 통신을 통한 방법 올리겠습니다 .
|