//生成json
JSONObject kker= new JSONObject();//服务器需要传参的json对象 person.put("pc", "2.0");//根据实际需求添加相应键值对 person.put("md", "post"); JSONObject address = new JSONObject(); JSONArray jsonArray=new JSONArray(); jsonArray.put("id").put("name"); address.put("output", jsonArray); JSONObject kker2=new JSONObject();
kker2.put("host","");address.put( "filter",person2);person.put( "params",address);person.put( "auth", "3 223");
person.put("id", 1);
Log.i("ooooo", person.toString());
//设置xutils请求
RequestParams params = new RequestParams(URL);添加请求地址 params.addHeader("Content-Type", "application/json-rpc"); 设置请求头部
params.setBodyContent(kker.toString());//添加json内容到请求参数里 // params.addParameter("as", "g");//添加参数 params.setAsJsonContent(true);//设置为json内容(这句个本人感觉不加也没有影响) x.http().post(params, new Callback.CacheCallback<String>() { @Override public void onSuccess(String result) {
//得到返回的数据result
Log.i("ooooo",result); try { JSONObject object=new JSONObject(result); JSONArray array=object.optJSONArray("result"); for(int i=0;i<array.length();i++){ // map=new HashMap<>(); JSONObject objects=array.optJSONObject(i); String hostid=objects.optString("id"); String name=objects.optString("name"); // map.put(hostid,name); data_list.add(name ); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } });