Japson_z 2015-06-18 12:23 采纳率: 0%
浏览 2499

使用HttpClient通过Post发送数据,如何显示到Web界面上

以前没有接触过android和服务器的交互,在上网找了一个Demo 试着做了一下,在android上写的数据可以在后台打印输出,但是如何在Web服务器上显示出来,求指教。代码如下:

 package com.example.m04_http02;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
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;

public class MainActivity extends Activity {

    private EditText nameText;
    private EditText pwdText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameText = (EditText) findViewById(R.id.nameText);
        pwdText = (EditText) findViewById(R.id.pwdText);
        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // 用户输入用户名密码, 然后通过Get方法发送给本地服务器
                String name = nameText.getText().toString();
                String pwd = pwdText.getText().toString();

                // 运行线程,使用GET方法向本地服务器发送数据
                GetThread getThread = new GetThread(name, pwd);
                getThread.start();
            }
        });
    }

    //子线程:通过GET方法向服务器发送用户名、密码的信息
    class GetThread extends Thread {

        String name;
        String pwd;

        public GetThread(String name, String pwd) {
            this.name = name;
            this.pwd = pwd;
        }

        @Override
        public void run() {
            //用HttpClient发送请求,分为五步
            //第一步:创建HttpClient对象
            HttpClient httpClient = new DefaultHttpClient();
            //注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
            String url = "http://192.168.1.112:8080/test.jsp?name=" + name+ "&password=" + pwd;
            //第二步:创建代表请求的对象,参数是访问的服务器地址
            HttpGet httpGet = new HttpGet(url);
            try {
                //第三步:执行请求,获取服务器发还的相应对象
                HttpResponse response = httpClient.execute(httpGet);
                //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
                if (response.getStatusLine().getStatusCode() == 200) {
                    //第五步:从相应对象当中取出数据,放到entity当中
                    HttpEntity entity = response.getEntity();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(entity.getContent()));
                    String result = reader.readLine();
                    Log.d("HTTP", "GET:" + result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

图片说明

  • 写回答

3条回答

  • 东东东雷 2015-06-19 01:57
    关注

    为什么要使用 httpclient 呢? 直接使用 servlet + jsp 进行通信不行吗? httpclient 是需要自己处理的。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog