android的HTTP的使用(httpClientPost()的使用)

效果图:

 

 

MainActivity

package com.huangfushi.hfs192025214_httpceshi;

import java.util.ArrayList;
import java.util.List;
/*引用apache . http相关类来建立HTTP联机*/
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
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.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os .Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener ;
import android .widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget .Toast;


public class MainActivity extends Activity {

    TextView tv = null;
    EditText editText1;
    EditText editText2;
    String shengao;
    String tizhong;
    String GetUrl= "http://www.meishihui68.com.cn/api/geo?latitude=39&longitude=116";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        tv = (TextView) findViewById(R.id.TextView02);
        Button httpget = (Button) findViewById(R.id.Button01);
        editText1 =(EditText)findViewById(R.id.editText1);
        editText2 =(EditText)findViewById(R.id.editText2);
        httpget.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                httpClientGet();

            }
        });
        Button httppost=(Button)findViewById(R.id.Button02);
        httppost.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               httpClientPost();

            }
        });
    }

    protected void httpClientGet() {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(GetUrl);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            String content = httpclient.execute(httpget, responseHandler);
            Toast.makeText(getApplicationContext(), "连接成功!",
                    Toast.LENGTH_SHORT).show();
//设置TextView
            tv.setText(content);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT)
                    .show();
            e.printStackTrace();
        }
        httpclient.getConnectionManager().shutdown();

    }



    protected void httpClientPost()
    {
        try
        {
        final String SERVER_URL = "http://www.meishihui68.com.cn/FormAjax.ashx";
        HttpPost request = new HttpPost(SERVER_URL);
        List params = new ArrayList();
            shengao=editText1.getText().toString();
            tizhong=editText2.getText().toString();
        params.add(new BasicNameValuePair("myheight", shengao));
        params.add(new BasicNameValuePair("myweight", tizhong));

        request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//设置参数的编码
        HttpResponse httpResponse = new DefaultHttpClient().execute(request);

        if (httpResponse.getStatusLine().getStatusCode() != 404)
        {
            String result = EntityUtils.toString(httpResponse.getEntity());
            tv.setText(result.toString());
    }
}catch (Exception e){
        }
        }
}

XML

<?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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.huangfushi.hfs192025214_httpceshi.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用HttpClient get访问GETAPI"
        android:id="@+id/Button01"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用HttpClient post访问"
        android:id="@+id/Button02"/>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView01"
        >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:text=" "
        android:id="@+id/TextView02"/>
    </ScrollView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="身高"/>
    <EditText
        android:layout_width="134dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="体重"/>
    <EditText
        android:layout_width="134dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"/>
</LinearLayout>

加入权限

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值