做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;
@Override
protected 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() {
@Override
public void onClick(View v) {
// 在子线程进行网络访问操作
new MyTask().execute();
}
});
}
class MyTask extends AsyncTask<Void, Void, String> {
@Override
protected 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;
}
@Override
protected 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 {
@Override
protected 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);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}</span>
效果图如下