android 网络请求 http

Http 请求分为 POST和GET这两种请求方式,在android开发中,我们可能会使用第三方的库进行网那个罗请求,但是这种基础的网络请求还是有必要学一下的,下面是我的实现代码:

public class HttpDownload {
    String uri = "https://api.thinkpage.cn/v2/weather/all.json?city=北京&language=zh-chs&unit=c&aqi=city&key=****";
    HttpGet httpGet;//***shi
    HttpClient httpClient;
    HttpResponse httpResponse;

    public String getContextFromURIByGet() {

        httpGet = new HttpGet(uri);
        httpClient = new DefaultHttpClient();
        try {
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
            httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            String result = "";
            while (null != (line = bufferedReader.readLine())) {
                result += line;
            }
            Log.e("test", "Http Response  result is " + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("test", "Http Response  error  URI is " + uri);
            return "Http Response  error  URI is " + uri;
        }


    }

    
    public JSONObject getContextFromURIByPost() {
        try {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
            httpPost.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", "benben");
            jsonObject.put("password", "123");

            StringEntity stringEntity = new StringEntity(jsonObject.toString());

            httpPost.setEntity(stringEntity);

            HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

            String result = EntityUtils.toString(httpResponse.getEntity());

            JSONObject jsonObjectResult = new JSONObject(result);

            return jsonObject;

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("test", "Http Response  error  URI is " + uri);
            return null;
        }

    }
}
activity :

public class MainActivity extends Activity {


    private TextView textView;

    Handler handler;
    {
        handler = new Handler() {


            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);


                switch (msg.what) {
                    case 1:
                        textView.setText(msg.getData().getString("get"));
                        break;
                }
            }
        };
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.hello);


        new Thread() {


            @Override
            public void run() {
                super.run();
                HttpDownload httpDownload = new HttpDownload();
                String result = httpDownload.getContextFromURIByGet();
                Message message = Message.obtain();
                message.what = 1;
                Bundle bundle = new Bundle();
                bundle.putString("get", result);
                message.setData(bundle);
                handler.sendMessage(message);
            }
        }.start();

这样就可以实现最原始的http请求了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值