◎위챗 : speedseoul
http://stackoverflow.com/questions/510995/parse-xml-response-with-jquery
http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=362969
http://seinarin.tistory.com/39
http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android
http://hayageek.com/android-http-post-get/#get
jsp때와 마찬가지로 데이터전송은 get방식과 post방식이 있습니다.
그래도 데이터가 공개되는 get방식보단 post방식을 사용하겠습니다.
먼저 login.jsp 이라는 jsp파일을 하나 생성합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("euc-kr"); String name = request.getParameter("name"); %> <% out.print(name);
System.out.println("login.jsp 호출됨");%>
액티비티에서 데이터 name을 보낼 예정이고, 다시 name이라는 출력결과를 가져갈 예정입니다.
그리고 접근이 되면 'login.jsp 호출됨' 이라고 서버에 출력이 됩니다.
이제 서버와 연결이기 때문에
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
이 코드를 사용할 예정입니다. 안그러면 오류나요~
우선
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_post" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="post" /> <TextView android:id="@+id/tv_post" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="" /> </LinearLayout>
MainActivity.java
package com.example.android_test1; import java.util.ArrayList; 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.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { TextView tv_post; Button btn_post; @SuppressLint({ "NewApi", "NewApi", "NewApi" }) public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_post = (TextView)findViewById(R.id.tv_post); btn_post = (Button)findViewById(R.id.btn_post); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); btn_post.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub String url = "http://xxx.xxx.xxx.xxx:xxxx/login.jsp"; HttpClient http = new DefaultHttpClient(); try { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("name", "유재석")); HttpParams params = http.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); HttpPost httpPost = new HttpPost(url); UrlEncodedFormEntity entityRequest = new UrlEncodedFormEntity(nameValuePairs, "EUC-KR"); httpPost.setEntity(entityRequest); HttpResponse responsePost = http.execute(httpPost); HttpEntity resEntity = responsePost.getEntity(); tv_post.setText( EntityUtils.toString(resEntity)); }catch(Exception e){e.printStackTrace();} }
}