HttpClient中的 Get 和 Post (一个待优化的WebUtil)

本文介绍了一种使用HTTP GET和POST方法,结合JSON格式进行新闻信息的获取与发布的实现过程,详细解释了如何通过Apache HttpClient库与JSON解析库完成这一任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.example.helloworld.NewsItem;


public class WebUtil {

         String myURL = "http://10.0.2.2:8080/helloworld/HelloData";

        public  List<NewsItem> getNewsInfo(){
            List<NewsItem> mList = new ArrayList<NewsItem>();

                try {
                    //HttpClient是一个接口,无法创建它的实例,通常情况下都会创建一个DefaultHttpClient的实例
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet =  new HttpGet(myURL);
                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        Log.i("yeye", "get运行了");
                        HttpEntity entity = httpResponse.getEntity();
                        String result = EntityUtils.toString(entity,"utf-8");
                        //JSON字符串解析
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject nObject = (JSONObject) jsonArray.get(i);
                                NewsItem news = new NewsItem();
                                news.setTitle(nObject.getString("myTitle"));
                                news.setContent(nObject.getString("myContent"));
                                mList.add(news);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return mList;

        }

        public void postNewsInfo(){
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2:8080/helloworld/PublishData");
            List<NameValuePair> mList = new ArrayList<NameValuePair>();
            mList.add(new BasicNameValuePair("title", "新标题呀"));
            mList.add(new BasicNameValuePair("content", "新内容呀"));

                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(mList,"utf-8"));
                    HttpResponse response = httpClient.execute(httpPost);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        Log.i("yeye", "post运行了");
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


        }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值