做Android有一段时间了,除了Android本身的一些组件以及API用的比较多外,开发接触比较多的就是网络这块了,而对于Android来说,我们只需要知道对应的接口就可以进行开发了,但是作为程序员,仅仅知道调用是不够的,所以这两天也对服务器进行简单的了解。

Android 客户端简单代码

别忘了添加网络权限

废话不多说,直接贴代码

xml:

<span style="font-size:14px;"><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"    android:padding="10dp" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="28dp"        android:text="Tips:请一定要在输入框输入内容" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="28dp"        android:orientation="horizontal" >        <TextView            android:id="@+id/tip"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="请在这里输入内容:" />        <EditText            android:id="@+id/input"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <Button        android:id="@+id/submit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="28dp"        android:text="点击提交" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="28dp"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="服务器返回的内容:" />        <TextView            android:id="@+id/show"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/hello_world" />    </LinearLayout></LinearLayout></span>
Activity代码:

<span style="font-size:14px;">package com.example.servlet;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.os.AsyncTask;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;import android.widget.TextView;public class MainActivity extends Activity {// 输入的内容private EditText inpuText;// 显示servlet返回的内容,也就是你输入的内容private TextView showTextView;// 提交到服务器按钮private Button submitButton;// 提交到服务器的参数String username;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);inpuText = (EditText) findViewById(R.id.input);submitButton = (Button) findViewById(R.id.submit);showTextView = (TextView) findViewById(R.id.show);submitButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 在子线程进行网络访问操作new MyTask().execute();}});}class MyTask extends AsyncTask<Void, Void, String> {@Overrideprotected String doInBackground(Void... params) {// 完整的url地址:// http://192.168.3.5:8080/HelloWorld/HelloWorld?username=xxxxx// 其中xxxxx为你输入的内容String urlString = "http://192.168.3.5:8080/HelloWorld/HelloWorld";// 192.168.3.5这个地址是你的电脑的IP地址username = inpuText.getText().toString();urlString = urlString + "?" + "username=" + username;String result = null;URL url = null;HttpURLConnection connection = null;InputStreamReader in = null;Log.e("TAG", "TAG1");try {url = new URL(urlString);Log.e("TAG", "TAG1");connection = (HttpURLConnection) url.openConnection();in = new InputStreamReader(connection.getInputStream());BufferedReader bufferedReader = new BufferedReader(in);StringBuffer strBuffer = new StringBuffer();String line = null;while ((line = bufferedReader.readLine()) != null) {strBuffer.append(line);}result = strBuffer.toString();} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}return result;}@Overrideprotected void onPostExecute(String result) {showTextView.setText(result);}}}</span>
Servlet代码:

<span style="font-size:14px;">package com.hpg.servlet;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 HelloWorld extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String usernameString = req.getParameter("username");resp.setContentType("text/html");PrintWriter out = resp.getWriter();// 可以在print中输入任何你想要输入的内容out.print("Success! Your username is " + usernameString);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubdoGet(req, resp);}}</span>

效果图如下




更多相关文章

  1. Android日志系统Logcat源代码简要分析
  2. 《Android应用开发揭秘》第九章Android特色开发一
  3. Android(安卓)FrameWork学习(二)Android系统源码调试
  4. 利用HTML5开发Android笔记
  5. Android源代码分析(三) MediaScanner源码分析(下)
  6. 【Android应用开发】-(7)安全性设计(完整版请参考官方)
  7. Android日志系统Logcat源代码简要分析
  8. Android(安卓)APP支持RTL模式
  9. Android当中的MVP模式(七)终篇---关于对MVP模式中代码臃肿

随机推荐

  1. android基础部分再学习---android处理运
  2. Android build system ---转
  3. 《Android开发艺术探索》第十章Android的
  4. Android doc 之应用程序基础
  5. Android 开发者在 Android Market 上兜售
  6. Android 零散知识点整理
  7. 【Android 开发】:数据存储之 SQLite 数据
  8. Windows上搭建Android开发环境详细教程
  9. 深入浅出 - Android系统移植与平台开发(十
  10. Android 开发者 for Flutter (1)Flutter和A