大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
网上有一个非常不同的情况来证明Android客户端和server如何结束的数据,但这些实施例大多相互作用更复杂,对于那些谁刚开始学习它是有害的,现在介绍一些简单的代码、逻辑清晰的互动样本,首先介绍本博客:
一、server端:
代码1:加入名为“AndroidServerServlet.java”的文件
package com.ghj.packageofservlet; 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 AndroidServerServlet extends HttpServlet { private static final long serialVersionUID = 6792396567928634227L; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/plain; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); System.err.println(request.getParameter("clientData")); PrintWriter printWriter = response.getWriter(); printWriter.print("您好Android客户端!"); printWriter.flush(); printWriter.close(); } }
代码2:改动名为“web.xml”的文件
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>AndroidServerServlet</servlet-name> <servlet-class>com.ghj.packageofservlet.AndroidServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AndroidServerServlet</servlet-name> <url-pattern>/AndroidServerServlet</url-pattern> </servlet-mapping></web-app>
二、Android手机客户端:
代码1:加入名为“AndroidClientActivity.java”的文件
package com.example.androidclient; import java.io.IOException; import java.io.UnsupportedEncodingException; 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.HttpClient; 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.os.Bundle; import android.os.StrictMode; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AndroidClientActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.android_client); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } Button sendButton = (Button) findViewById(R.id.send_button); sendButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { HttpPost httpRequest = new HttpPost("http://172.16.99.207:8080/AndroidServer/AndroidServerServlet"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("clientData", "您好server端!")); try { httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置请求參数项 HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest);//运行请求返回响应 if(httpResponse.getStatusLine().getStatusCode() == 200){//推断是否请求成功 Toast.makeText(AndroidClientActivity.this, EntityUtils.toString(httpResponse.getEntity()), Toast.LENGTH_LONG).show(); }else{ Toast.makeText(AndroidClientActivity.this, "没有获取到Androidserver端的响应。", Toast.LENGTH_LONG).show(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); }}
注意:请注意“AndroidClientActivity.java”文件里34、35、36和37这4行代码——假设没有这几行代码,则会出现android.os.NetworkOnMainThreadException异常,关于该异常请參见还有一篇名为《
android.os.NetworkOnMainThreadException异常》博客
代码2:加入名为“android_client.xml”的文件
<RelativeLayout 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: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" > <Button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_server" /> </RelativeLayout>
【0点下载Demo】
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117781.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...