http请求

package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button fasong;//发送请求

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fasong = (Button) findViewById(R.id.button);
        fasong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        parename();
                    }
                }).start();
            }
        });
    }


    public void parename() {

        HttpClient httpClient = new DefaultHttpClient();

//定义与服务器交互的地址

        String ServerUrl = "";//请求的网址

//设置读取超时,注意CONNECTION_TIMEOUT和SO_TIMEOUT的区别

        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);

//设置读取超时

        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

//POST方式

        HttpPost httpRequst = new HttpPost(ServerUrl);
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();//携带的类容
        params.add(new BasicNameValuePair("username", ""));
        params.add(new BasicNameValuePair("password", "123456"));
        params.add(new BasicNameValuePair("type", "1"));
        params.add(new BasicNameValuePair("device", ""));

        try {

            httpRequst.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

//得到响应

            HttpResponse response = httpClient.execute(httpRequst);

//返回值如果为200的话则证明成功的得到了数据

            if (response.getStatusLine().getStatusCode() == 200)

            {

                StringBuilder builder = new StringBuilder();

//将得到的数据进行解析

                BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

//readLine()阻塞读取

                for (String s = buffer.readLine(); s != null; s = buffer.readLine())

                {

                    builder.append(s);

                }


                System.out.println(builder.toString());

//得到Json对象
                JSONObject jsonObject = new JSONObject(builder.toString());

//通过得到键值对的方式得到值

                int CmdId = jsonObject.getInt("status");

                String SResult = jsonObject.getString("uid");

                String SUserName = jsonObject.getString("username");

                int SResultPara = jsonObject.getInt("iswrite");
                System.out.println(CmdId + SResult + SUserName + SResultPara);

            } else {


            }

        } catch (Exception e)

        {

            e.printStackTrace();

        }

        return;

    }

}
 
 

 
在build 加入
android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ASL2.0'
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值