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

来源:互联网 发布:同志软件有哪些 编辑:程序博客网 时间:2024/06/07 10:09
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();        }      }


原创粉丝点击