如何往服务器中读写数据?

本次项目,我用apache-tomcat将自己的计算机弄成了一个小服务器,然后对里面的jsp类型的文件进行读写。

首先,如何弄服务器呢?工程文件:点击打开链接

1.下载一个apache-tomcat,这里我给大家提供一个apache-tomcat-6.0.37的下载地址:点击打开链接   提取码:utb8

2.下载好之后解压,解压完成,进入文件夹,运行apache-tomcat-6.0.37/bin/startup.dat  如下图所示。




3.打开该文件之后,等待。等到进程完成后。打开你的浏览器,在地址栏中键入:localhost:8080   

跳转,如果可以正常访问到有猫的界面,那么恭喜,服务器已建立。

服务器建好了,那么该读取其中的数据了。

这里的布局我设了一个EditView,用于输入IP地址,两个按钮,分别是不同的获取信息的方式,而文本框,则是为了显示读取的信息。

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="jerehdu.com.jereheduch10.HttpActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IP地址"
        android:textSize="20sp"
        android:textColor="#123"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        android:hint="请输入IPv4地址"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/search1"
        android:text="查询(Get方式)"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/search2"
        android:text="查询(Post方式)"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/show"
        android:text="查询结果"
        android:background="#e9d6ec"/>
</LinearLayout>
接下来是Acticity,里面有详细的注释,我就不在这里说了。

public class HttpActivity extends AppCompatActivity {

    private EditText et ;
    private Button search1;
    private Button search2;
    private TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        et = (EditText) findViewById(R.id.et);
        search1 = (Button) findViewById(R.id.search1);
        search2 = (Button) findViewById(R.id.search2);
        show = (TextView) findViewById(R.id.show);
        search1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url =
                        "http://"+et.getText()+":8080/HttpTest/index.jsp" +
                                "?option=getUser&uName=jerehedu";
                new MyGetJob().execute(url);
            }
        });
        search2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] arg = new String[2];
                arg[0] = "http://"+et.getText()+":8080/HttpTest/index.jsp";
                arg[1] = "option=getUser&uName=jerehedu";
                new MyPostJob().execute(arg);
            }
        });
    }

    public class MyPostJob extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            HttpURLConnection httpURLConnection = null;
            InputStream is = null;
            StringBuilder sbd = new StringBuilder();
            try {
                URL url = new URL(strings[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(5*1000);
                httpURLConnection.setReadTimeout(5*1000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                //是否保存用户缓存
                httpURLConnection.setUseCaches(false);
                //在这里设置编码方式
                httpURLConnection.setRequestProperty("Charset","UTF-8");
                httpURLConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
                //params应该是这样的样式: option=getUser&Name=jerehedu  (没了问号)
                String params = strings[1];
                OutputStream os = httpURLConnection.getOutputStream();
                os.write(params.getBytes());
                os.flush();
                os.close();

                if(httpURLConnection.getResponseCode()==200){
                    is=httpURLConnection.getInputStream();
                    int next = 0;
                    byte[] bt = new byte[1024];
                    while ((next=is.read(bt))>0){
                        sbd.append(new String(bt,0,next));
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(httpURLConnection!=null){
                    httpURLConnection.disconnect();
                }
            }
            return sbd.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            show.setText("POST请求结果:"+s);

        }
    }
    /*
    AsyncTask异步任务类
      异步任务类的第一个参数会传到doInBackground方法中
      第三个参数:
          此为指定doInBackground方法的返回值。
          onPostExecute方法中的String s是来自doInBackground方法的返回值。
      UI线程里声明的任何变量,在子线程中禁止操作。

     异步任务类里只有5个方法。

     */
    public class MyGetJob extends AsyncTask<String,Void,String>{

        //onPreExecute在主线程中执行命令,它会在doInBackground前执行。
        //通常会在这里做进度条的初始化,例如:下载进度
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        //必须实现的方法,可以理解成,在子线程中执行命令
        @Override
        //这里的...代表数组
        protected String doInBackground(String... strings) {
            HttpURLConnection httpURLConnection = null;
            InputStream is = null;
            StringBuilder sbd = new StringBuilder();
            try {
                URL url = new URL(strings[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(5*1000);
                httpURLConnection.setReadTimeout(5*1000);
                 /*
                    http 响应码
                    200:成功
                    404:未找到
                    500:发生错误
                     */
                if(httpURLConnection.getResponseCode()==200){
                    is=httpURLConnection.getInputStream();
                    int next = 0;
                    byte[] bt = new byte[1024];
                    while ((next=is.read(bt))>0){
                        sbd.append(new String(bt,0,next));
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(httpURLConnection!=null){
                    httpURLConnection.disconnect();
                }
            }
            return sbd.toString();
        }

        //在UI线程(主线程)中执行命令
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute("GET请求结果:"+s);
            show.setText(s);
        }
    }
}
在运行的时候,需要我们输入IP地址,即我们服务器的IP地址,怎么获得呢?

首先我们打开“运行”——Windows+R,然后输入CMD,确认。之后再输入ipconfig,回车。则就可以看到IP地址了,如下图所示:




输入IP地址,点击按钮。效果图如下:



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值