AndroidネットワークプログラミングはHttpCientを使ってウェブサイトにアクセスします。
4471 ワード
Http ClienntDemo.javaインターフェースは二つのボタンと一つのテキストボックスです。
/*
* HttpClientlai ,
* A, GET
* 1, HttpClient ;HttpClient httpclient=new DefaultHttpClient();
* 2, GET , HttpGet :HttpGet httpget=new HttpGet("http://www.baidu.com");
* 3, HttpClient HttpGet HttpResponse , HttpResponse :
* HttpResponse httpresponse=httpclient.execute(httpget);
* 4, httpresponse Http
HttpEntity entity=httpresponse.getEntity();
* */
public class HttpClientDemo extends Activity {
TextView response;
// HttpClient
HttpClient httpclient;
Handler handler=new Handler(){
public void handleMessage(Message msg){
if(msg.what==0x123){
// response
response.append(msg.obj.toString()+"
");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_client);
//1, DefaultHttpClient , HttpClient
httpclient=new DefaultHttpClient();
response=(TextView) findViewById(R.id.response);
}
/*
* GET
*
* */
public void accessSecret(View v){
response.setText("");
// , , Get
new Thread(){
public void run(){
//2, HttpGet
HttpGet httpget=new HttpGet("http://localhost:8080/foo/secret.jsp");//jsp To cat
try {
//3, HttpClient HttpGet HttpResponse , HttpResponse
HttpResponse httpresponse=httpclient.execute(httpget);
//4, httpresponse Http
HttpEntity entity=httpresponse.getEntity();
if(entity!=null){
//5,entity , ,
BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent()));
String line=null;
while((line=br.readLine())!=null){//
Message msg=new Message();
msg.what=0x123;
msg.obj=line;
handler.sendMessage(msg);// UI UI
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
/*
* Post
*
*
* */
public void showLogin(View v){
final View loginDialog=getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(HttpClientDemo.this)
.setTitle(" ")
.setView(loginDialog)
.setPositiveButton(" ", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//
final String name=((EditText)loginDialog.findViewById(R.id.name)).getText().toString();
final String pass=((EditText)loginDialog.findViewById(R.id.pass)).getText().toString();
// , , Post
new Thread(){
public void run(){
try {
//2, HttpPost
HttpPost httppost=new HttpPost("http://localhost:8080/foo/login.jsp");//jsp To cat
//3, ,NameValuePair
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name",name));//
params.add(new BasicNameValuePair("pass",pass));
//3,
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//4,HttpClient HttpPost ,
HttpResponse httpresponse=httpclient.execute(httppost);
//5, 200
if(httpresponse.getStatusLine().getStatusCode()==200){
//200: ,301/302: ,404:not found ,501 ,
String msg = EntityUtils.toString(httpresponse.getEntity());
Looper.prepare();
//
Toast.makeText(HttpClientDemo.this, msg, Toast.LENGTH_LONG).show();
Looper.loop();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}).setNegativeButton(" ", null).show();
}