Android Apps 网络通信(http json/array请求 )

import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;    

public static JSONObjectpostJson_JSONObject(String url,Map<String,Object> params){
        JSONObject jsonObject = null;
        try {
            HttpPost request = new HttpPost(url);
            JSONObject param = new JSONObject();
            if(params !=null){
                Set<Entry<String,Object>>set = params.entrySet();
                Iterator<Entry<String,Object>>it = set.iterator();
                while(it.hasNext()){
                    Entry<String, Object>entry = it.next();
                    String key = entry.getKey();
                    Object object =entry.getValue();
                    param.put(key,object);
                }
            }
            StringEntity se = new StringEntity(param.toString());
            request.setEntity(se);
            HttpResponse httpResponse = new DefaultHttpClient().execute(request);
            String retSrc = EntityUtils.toString(httpResponse.getEntity());
            jsonObject = new JSONObject(retSrc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

这里只能post简单格式的json 如:

{

  "name":"android",

  "age":"22"

}


如果是复杂的Json可以直接转为字符串 如:

{

    "BasicInfo": [

        {

            "License_cate": "澳門車牌",

            "Interior": "0",

            "License": "MI-6",

            "Car_status": "七成新",

            "Transmission": "自動波",

            "Type": "120i M/T",

            "Price": "10~20萬",

            "Landing_time": "2002",

            "Maintain_status": "0",

            "Producer": "德國",

            "Company": "寶馬(BMW",

            "Car_color": "white",

            "Allocation": "0"

        },

        {

            "License_cate": "澳門車牌",

            "Interior": "0",

            "License": "MI-6",

            "Car_status": "七成新",

            "Transmission": "自動波",

            "Type": "120i M/T",

            "Price": "10~20萬",

            "Landing_time": "2002",

            "Maintain_status": "0",

            "Producer": "德國",

            "Company": "寶馬(BMW",

            "Car_color": "white",

            "Allocation": "0"

        }

    ]

}


代码:

public void postCarJson() throws Exception{
       
        JSONObject object = new JSONObject();
        object.put("Company", "寶馬(BMW");
        object.put("Type","120i M/T");
        object.put("License","MI-6");
        object.put("Car_color","white");
        object.put("Transmission","自動波");
        object.put("Producer","德國");
        object.put("License_cate","澳門車牌");
        object.put("Landing_time","2002");
        object.put("Interior","0");
        object.put("Car_status","七成新");
        object.put("Maintain_status","0");
        object.put("Price","10~20萬");
        object.put("Allocation","0");
       
        Map<String,Object> param = new HashMap<String,Object>();
       
        JSONArray array = new JSONArray();
        array.put(object.toString());
        array.put(object.toString());
       
        //System.out.println(array.toString());
        //System.out.println(object.toString());
        param.put("BasicInfo",array.toString());
        //JSONObject result =HttpJsonUtil.postJson_JSONObject(reUr7, param);
        //System.out.println(result.toString());
        String post = "{"+"\""+"BasicInfo" +"\""+":"+array.toString()+"}";
        try {
            byte[] buffer = post.getBytes("UTF-8");
            URLu = new URL(reUr7);
            HttpURLConnection conn =(HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Length", buffer.length+"");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
       
            OutputStream out =conn.getOutputStream();
            out.write(buffer);
            out.close();
            InputStream in =conn.getInputStream();
            StringBuffer sBuffer = new StringBuffer();
            byte[] buf = new byte[1024];
            for(int n; (n=in.read(buf)) != -1;){
                sBuffer.append(new String(buf, 0,n,"utf-8"));
            }
            Log.i("response", sBuffer.toString());
           
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学英语的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值