Android 客户端访问服务器以及服务器开发
16lz
2021-01-23
做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客户端与PC服务器实现Socket通信(wifi)
- Android源代码分析(三) MediaScanner源码分析(下)
- Android当中的MVP模式(七)终篇---关于对MVP模式中代码臃肿
- Android 中如何将带有html格式的文章转换成能在Android中显示的
- android:layout_marginLeft指该控件距离边父控件的边距, android:
- android 通过lint以及android-resource-remover清楚不用的资源以
- Android 获取无线蓝牙MAC信息代码