android apache HTTP demo 互联网访问
16lz
2021-01-23
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 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);}//resetListenerprivate OnClickListener resetListener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubusername.setText("");password.setText("");}};//submitListenerprivate OnClickListener submitListener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString _username = username.getText().toString();String _password = password.getText().toString();Log.i("android servlet", _username+" "+_password);login(_username,_password);}};//showDialogprivate void showDialog(String msg){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage(msg).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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);}}
更多相关文章
- android之实现各个组件点击事件处理
- Android UI开发第十七篇――Android Fragment实例
- Android Studio将so打包jar供其他项目引用
- android之实现SeekBar拖动组件
- android自学笔记 开始--->第一个应用--->第一个项目
- android 自定义组件圆形边框
- 如何手工创建android项目
- Android 的自定义Spinner组件实现方式