做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>

效果图如下

Android 客户端访问服务器以及服务器开发_第1张图片


更多相关文章

  1. Android客户端与PC服务器实现Socket通信(wifi)
  2. Android源代码分析(三) MediaScanner源码分析(下)
  3. Android当中的MVP模式(七)终篇---关于对MVP模式中代码臃肿
  4. Android 中如何将带有html格式的文章转换成能在Android中显示的
  5. android:layout_marginLeft指该控件距离边父控件的边距, android:
  6. android 通过lint以及android-resource-remover清楚不用的资源以
  7. Android 获取无线蓝牙MAC信息代码

随机推荐

  1. 【Android】Conversion to Dalvik format
  2. 匿名类中this的特殊用法(class.this)
  3. Android JNI 之 Bitmap 操作
  4. android的apk包中的xml文件打开方法
  5. android 利用重力感应监测手机摇晃,做出相
  6. robotium获取本地文档内容
  7. AndroidUi(1)-直线
  8. ubuntu 使用android studio 查看android(
  9. Android开发的小细节
  10. MTK 平台(MTK6573)马达驱动