android get方法的使用方法,Android开发之使用Get方法向服务器发送请求-Fun言

xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:paddingBottom=”@dimen/activity_vertical_margin”

android:paddingLeft=”@dimen/activity_horizontal_margin”

android:paddingRight=”@dimen/activity_horizontal_margin”

android:paddingTop=”@dimen/activity_vertical_margin”

tools:context=”.MainActivity” >

android:id=”@+id/userId”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:hint=”username” />

android:id=”@+id/pwdId”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:hint=”password”

android:layout_below=”@id/userId”/>

android:id=”@+id/submitId”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=”submit”

android:layout_below=”@id/pwdId”

/>

package com.example.httpgetandpost;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.entity.InputStreamEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BufferedHeader;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {

private EditText namEditText;

private EditText pwdEditText;

private Button submitbutton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

namEditText = (EditText)findViewById(R.id.userId);

pwdEditText = (EditText)findViewById(R.id.pwdId);

submitbutton = (Button)findViewById(R.id.submitId);

submitbutton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

String name = namEditText.getText().toString();

String pwd = pwdEditText.getText().toString();

GetThread gt=new GetThread(name,pwd);

gt.start();

}

});

}

class GetThread extends Thread{

String name;

String pwd;

public GetThread(String name,String pwd){

this.name = name;

this.pwd = pwd;

}

@Override

public void run() {

HttpClient httpClient = new DefaultHttpClient();

String url=”http://192.138.4.75:8080/s02e14.jsp?name=”+name+”&password=”+pwd

HttpGet httpGet = new HttpGet(url);

try {

HttpResponse resp=httpClient.execute(httpGet);

if (resp.getStatusLine().getStatusCode()==200) {

HttpEntity entity = resp.getEntity();

BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));

String result = reader.readLine();

//System.out.println(“HTTP”+result);

System.out.println(“HTTTTTTTTTTTTTTP”+result);

Log.d(“HTTP”,result);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值