您的位置:首页 > Web前端 > JavaScript

学习json post请求和json解析

2015-02-09 13:44 375 查看
post请求

/** 

     * 发送HttpPost请求 

     *  

     * @param strURL 

     *            服务地址 

     * @param params 

     *            json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/> 

     * @return 成功:返回json字符串<br/> 

     */  

    public static String post(String strURL, String params) {  

        System.out.println(strURL);  

        System.out.println(params);  

        try {  

            URL url = new URL(strURL);// 创建连接  

            HttpURLConnection connection = (HttpURLConnection) url  

                    .openConnection();  

            connection.setDoOutput(true);  

            connection.setDoInput(true);  

            connection.setUseCaches(false);  

            connection.setInstanceFollowRedirects(true);  

            connection.setRequestMethod("POST"); // 设置请求方式  

            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式  

            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式  

            connection.connect();  

            OutputStreamWriter out = new OutputStreamWriter(  

                    connection.getOutputStream(), "UTF-8"); // utf-8编码  

            out.append(params);  

            out.flush();  

            out.close();  

            // 读取响应  

            int length = (int) connection.getContentLength();// 获取长度  

            InputStream is = connection.getInputStream();  

            if (length != -1) {  

                byte[] data = new byte[length];  

                byte[] temp = new byte[512];  

                int readLen = 0;  

                int destPos = 0;  

                while ((readLen = is.read(temp)) > 0) {  

                    System.arraycopy(temp, 0, data, destPos, readLen);  

                    destPos += readLen;  

                }  

                String result = new String(data, "UTF-8"); // utf-8编码  

                System.out.println(result);  

                return result;  

            }  

        } catch (IOException e) {  

            // TODO Auto-generated catch block  

            e.printStackTrace();  

        }  

        return "error"; // 自定义错误信息  
    }  

json解析(不通用根据实际情况修改)

 public  ArrayList<Tab_Activity> jsonToArray(String jsonStr){

    JSONObject dataJson;

    Tab_Activity mTab_Activity = null;

    ArrayList<Tab_Activity> mActivityList = new ArrayList<Tab_Activity>();
try {
dataJson = new JSONObject(jsonStr);

// JSONObject  response = dataJson.getJSONObject("Output");
// 获得数组个数
int count = dataJson.getInt("count");
Log.i("tjj", "count = "+count);
JSONArray data = dataJson.getJSONArray("Activities");
Log.i("tjj", "data = "+data.length());
// 循环创建json对象
JSONObject[] info = new JSONObject[count];
for(int i = 0; i < count; i++){
info[i] = data.getJSONObject(i);
Log.i("tjj", "info["+i+"] = "+info[i].length());
// 将获得的信息记录
mTab_Activity = new Tab_Activity();

mTab_Activity.setId(info[i].getInt("id"));
Log.d("tjj", mTab_Activity.getId()+"");
mTab_Activity.setImage_url(info[i].getString("imageUrl"));
Log.d("tjj", mTab_Activity.getImage_url());
mTab_Activity.setAction(info[i].getString("action"));
Log.d("tjj", mTab_Activity.getAction());
mTab_Activity.setAct_des(info[i].getString("actDes"));
Log.d("tjj", mTab_Activity.getAct_des());
mTab_Activity.setAct_number(info[i].getInt("actNumber"));
Log.d("tjj", mTab_Activity.getAct_number()+"");
mTab_Activity.setAct_type(info[i].getInt("actType"));
Log.d("tjj", mTab_Activity.getAct_type()+"");

// mTab_Activity.setAct_start_time(null);

// mTab_Activity.setAct_end_time(info[i].ge);

mActivityList.add(mTab_Activity);
}
return mActivityList;

// String actCode=info[0].getString("province");

// String city=info[0].getString("city");

// String district=info[0].getString("district");

// String address=info[0].getString("address");

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json json解析