2015.1.9,第六次课Android对服务端数据进行json解析

时间:2021-09-11 19:46:07

这里写了两种解析方法

package com.example.activity;


import java.util.List;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;


public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v){
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    asyncHttpClient.get(
"http://192.168.1.74:8080/jijieproject/UsersServlet",
new AsyncHttpResponseHandler() {
@Override
@Deprecated
public void onSuccess(String content) {
Log.e("aaaa", content);

try {
JSONArray jsonArray=new JSONArray(content);
    for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject=jsonArray.getJSONObject(i);
String account=jsonObject.getString("account");
String company_identification=jsonObject.getString("company_identification");
String photo_url=jsonObject.getString("photo_url");
Log.i("photo_url", photo_url);
}
} catch (JSONException e) {
e.printStackTrace();
}


super.onSuccess(content);
}


@Override
@Deprecated
public void onFailure(Throwable error) {
Log.e("MY", error.getMessage());
super.onFailure(error);
}
});
    }
    

    

第二种方法

第二种方法,需要借住实体类

一下提供了接口,和相应的数据

    public void click1(View v){
   
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    AsyncHttpResponseHandler asyncHttpResponseHandler=new AsyncHttpResponseHandler() {
@Override
@Deprecated
// {
//    "rows": "10",
//    "info": "免费接口随机延迟1-8分钟。购买或试用付费接口请访问api.opencai.net或加QQ:9564384(注明彩票API)",
//    "code": "cqssc",
//    "data": [
//        {
//            "expect": "20150109051",
//            "opencode": "5,9,3,2,9",
//            "opentime": "2015-01-09 14:31:00",
//            "opentimestamp": "1420785060177"
//        },
//        {
//            "expect": "20150109042",
//            "opencode": "9,9,1,2,6",
//            "opentime": "2015-01-09 13:00:46",
//            "opentimestamp": "1420779646787"
//        }
//    ]
// }
public void onSuccess(String content) {
Group group=JSON.parseObject(content, Group.class);
String rows=group.getRows();
String info=group.getInfo();
String code=group.getCode();
   List<Data>lists=group.getData();
  for (Data data : lists) {
String expect=data.getExpect();
String opencode=data.getOpencode();
String opentime=data.getOpentime();
String opentimestamp=data.getOpentimestamp();
    }
  Log.e("aaaaaaaaaaaaaaaaa", lists.size()+"");
super.onSuccess(content);
}

@Override
@Deprecated
public void onFailure(Throwable error) {
super.onFailure(error);
}
};
    asyncHttpClient.get(
"http://f.opencai.net/cqssc-10.json"
,asyncHttpResponseHandler);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

}



实体类

package com.example.activity;


import java.util.List;


public class Group {
private String rows;
private String info;
private String code;
private List<Data> data;
public String getRows() {
return rows;
}
public void setRows(String rows) {
this.rows = rows;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}




}


实体类

package com.example.activity;


public class Data {
private String expect;
private String opencode;
private String opentime;
private String opentimestamp;
public String getExpect() {
return expect;
}
public void setExpect(String expect) {
this.expect = expect;
}
public String getOpencode() {
return opencode;
}
public void setOpencode(String opencode) {
this.opencode = opencode;
}
public String getOpentime() {
return opentime;
}
public void setOpentime(String opentime) {
this.opentime = opentime;
}
public String getOpentimestamp() {
return opentimestamp;
}
public void setOpentimestamp(String opentimestamp) {
this.opentimestamp = opentimestamp;
}






}

                                                                                                                                                                                                                                            微天下站长:jSmen_LBG发布