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;

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐