1、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.getserverdata.MainActivity" > <EditText
android:id="@+id/et_username"
android:hint="请输入用户名"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText> <EditText
android:id="@+id/et_password"
android:hint="请输入密码"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" /> <Button
android:onClick="click1"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="get方式登录" /> <Button
android:onClick="click2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="post方式登录" /> </LinearLayout>
2.AndroidManifest.xml 配置权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getserverdata"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
3.get和post请求
package com.example.getserverdata.service; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import com.example.getserverdata.utils.StreamUtil; public class LoginService { public static String loginByGet(String username,String password)
{ String path = "http://192.168.1.100:8088/Login.ashx?username="+username+"&password="+password; try {
//创建URL
URL url = new URL(path); //创建http连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置连接时间
conn.setConnectTimeout(5000);
//设置请求方式
conn.setRequestMethod("GET"); //获取请求码
int code = conn.getResponseCode(); System.out.println("code:"+code); if(code==200)
{
//请求成功
//获取响应数据
InputStream is = conn.getInputStream();
//得到响应数据
String result = StreamUtil.readInputStream(is); return result;
}
else
{
//请求失败
return null;
} } catch (Exception e) {
e.printStackTrace();
} return null;
} public static String loginByPost(String username,String password)
{ String path = "http://192.168.1.100:8088/Login.ashx"; try {
//创建URL
URL url = new URL(path); //创建http连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置连接时间
conn.setConnectTimeout(5000);
//设置请求方式
conn.setRequestMethod("POST"); //准备数据
String data = "username="+username+"&password="+password;
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Lenght", data.length()+""); //post方式 实际上是浏览器把数据写给了服务器
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(data.getBytes()); //获取请求码
int code = conn.getResponseCode(); System.out.println("code:"+code); if(code==200)
{
//请求成功
//获取响应数据
InputStream is = conn.getInputStream();
//得到响应数据
String result = StreamUtil.readInputStream(is); return result;
}
else
{
//请求失败
return null;
} } catch (Exception e) {
e.printStackTrace();
} return null;
}
}
4.将InputStream转为String
package com.example.getserverdata.utils; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; public class StreamUtil { public static String readInputStream(InputStream is)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while((len = is.read(data))!=-1)
baos.write(data, 0, len);
is.close();
baos.close();
return new String(baos.toByteArray()); } catch (Exception e) { e.printStackTrace();
} return null;
}
}
5.MainActivity
package com.example.getserverdata; import com.example.getserverdata.service.LoginService; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { private EditText et_username;
private EditText et_password; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_username = (EditText)findViewById(R.id.et_username);
et_password = (EditText)findViewById(R.id.et_password);
} public void click1(View view)
{ final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim(); new Thread(){ public void run(){ final String result = LoginService.loginByGet(username, password); System.out.println("reuslt:"+result); if(result!=null)
{
runOnUiThread(new Runnable(){ @Override
public void run() { Toast.makeText(MainActivity.this, result, 0).show();
} }); } else
{
runOnUiThread(new Runnable(){ @Override
public void run() {
Toast.makeText(MainActivity.this, "获取数据失败", 0).show();
}
}); } } }.start(); } public void click2(View view)
{ final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim(); new Thread(){ public void run(){ final String result = LoginService.loginByPost(username, password); System.out.println("reuslt:"+result); if(result!=null)
{
runOnUiThread(new Runnable(){ @Override
public void run() { Toast.makeText(MainActivity.this, result, 0).show();
} }); } else
{
runOnUiThread(new Runnable(){ @Override
public void run() {
Toast.makeText(MainActivity.this, "获取数据失败", 0).show();
}
}); } } }.start(); } }