使用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();
            }

        }
    }
}

图片说明

查看全部
japson_iot
Japson_z
2015/06/18 12:23
  • http服务器
  • android
  • 服务器
  • 发送数据
  • 点赞
  • 收藏
  • 回答
    私信

3个回复