도전2022
안드로이드 에서 Thread를 이용하여 HttpClient 하기 본문
무지 에러가 발생되다가 이제 되는것 같다. 휴~~
package com.example.test02;
import java.util.ArrayList;
import java.util.List;
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.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById( R.id.submitBtn ) ;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// String user_id = ((EditText) findViewById(R.id.user_id)).getText().toString() ;
// String user_pwd = ((EditText) findViewById(R.id.user_pwd)).getText().toString() ;
TextView resultField = ( TextView ) findViewById( R.id.get_result ) ;
Thread thread1 = new Thread(new Runnable()
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
public void run()
{
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://threej.co.kr/smc/20140203test_mysql.php";
HttpPost post = new HttpPost(postURL);
String user_id = ((EditText)findViewById(R.id.user_id)).getText().toString() ;
String user_pwd = ((EditText)findViewById(R.id.user_pwd)).getText().toString() ;
params.add(new BasicNameValuePair("my", user_id));
params.add(new BasicNameValuePair("data", user_pwd));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,"EUC-KR");//HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null)
{
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
}
catch (Exception e)
{
}
//resultField.setText("error");
}
});
thread1.start();
}
});
}
}
'작업 > work2014' 카테고리의 다른 글
Windows SDK for Windows 7 설치 후, 문서 (0) | 2014.02.13 |
---|---|
WDK and WinDbg downloads (0) | 2014.02.12 |
패턴인식2014 (0) | 2014.02.03 |
디자인들 (0) | 2014.01.30 |
77GHz Radar 찾기 (0) | 2014.01.30 |