android apache HTTP demo 互联网访问
android用户名:________
密码:________
submitButton,resetButton
(模拟器上访问地址填写
http://10.0.2.2:8080/test/servlet/androidServlet
不用写127.0.0.1或localhost
)
第二部:建立个web项目的一个servlet,接受android请求
package com.isoftstone.cry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Entity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity
{
private Button submit , reset ;
private EditText username , password ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
//实例化组件
username = (EditText)findViewById(R.id.loginName);
password = (EditText)findViewById(R.id.loginPsw);
submit = (Button)findViewById(R.id.submit);
reset = (Button)findViewById(R.id.reset);
//添加监听
reset.setOnClickListener(resetListener);
submit.setOnClickListener(submitListener);
}
//resetListener
private OnClickListener resetListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
username.setText("");
password.setText("");
}
};
//submitListener
private OnClickListener submitListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String _username = username.getText().toString();
String _password = password.getText().toString();
Log.i("android servlet", _username+" "+_password);
login(_username,_password);
}
};
//showDialog
private void showDialog(String msg)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog alert = builder.create();
alert.show();
}
//login method
private void login(String _username,String _password)
{
String strUrl = "http://10.0.2.2:8080/test/servlet/androidServlet";
HttpPost request = new HttpPost(strUrl);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name", _username));
parameters.add(new BasicNameValuePair("psw", _password));
try {
request.setEntity(new UrlEncodedFormEntity(parameters,HTTP.UTF_8));
HttpResponse response = new DefaultHttpClient().execute(request);
if(response.getStatusLine().getStatusCode()==200){
String msg = EntityUtils.toString(response.getEntity());
showDialog(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username" />
<EditText
android:id="@+id/loginName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />
<EditText
android:id="@+id/loginPsw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset" />
</TableRow>
</TableLayout>
</LinearLayout>
添加访问权限
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
test项目
package com.isoftstone.cry;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class androidServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("****************/servlet/androidServlet*************");
String name = request.getParameter("name");
String psw = request.getParameter("psw");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("login success");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
页:
[1]