如何显示数据?

时间:2016-08-08 12:44:26

标签: android asp.net-web-api

我正在使用此方法在我的应用程序中调用我的服务。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);
    String url = "url";

    AQuery mAQuery = new AQuery(Next.this);
    mAQuery.ajax(url, String.class, new AjaxCallback<String>() {
        @Override
        public void callback(String url, String data, AjaxStatus status) {
            super.callback(url, data, status);
            if (BuildConfig.DEBUG) {
                Log.d("###$Request URL", url + "");
                Log.d("###$Response ", data + "");
                Log.d("###$Status Message : ", status.getMessage() + "");
                Log.d("###$Status Code : ", status.getCode() + "");

            }


            if (null != data && status.getCode() != -101) {

                String StringData = "" + data;
                try {


                    JSONObject json = new JSONObject(StringData);

                    String COMP_REQ_ID = json.getString("COMP_REQ_ID");
                    String CompanyName = json.getString("CompanyName");
                    String COMP_REQ_TYPE = json.getString("COMP_REQ_TYPE");
                    String Name  = json.getString("Name ");

                    myAwesomeTextview.setText("COMP_REQ_ID: " + COMP_REQ_ID + "\n" + "CompanyName:" + CompanyName + "\n" + "COMP_REQ_TYPE: " + COMP_REQ_TYPE + "\n" + "Name : " + Name);

                } catch (JSONException e) {
                    myAwesomeTextview.setText("" + e);
                }

但是来自服务器的数据没有显示在我的手机屏幕上。 我从我的服务中获得的数据如下:

              [{"Name":null,"PositionName":null,"DateOfEvent":null,"EvetnId":0,"HetId":0,"EventDate":null,"COMP_REQ_ID":9714,"COMP_REQ_TYPE":"Intership","JobTitle":"Administrator","CompanyName":"Jensor's International (Ltd).","ReqQualification":"","DegreeName":"B.E/B.Tech,M.C.A,M.B.A,B.A,B.A.M.S,B.Com,B.S.W","Post_Status":1,"Eventdate":"21/06/2016","JobsOrInternships":null},{"Name":null,"PositionName":null,"DateOfEvent":null,"EvetnId":0,"HetId":0,"EventDate":null,"COMP_REQ_ID":9713,"COMP_REQ_TYPE":"Intership","JobTitle":"junior counselor","CompanyName":"Jensor's International (Ltd).","ReqQualification":"","DegreeName":"B.E/B.Tech,M.C.A,M.B.A,B.B.M,B.Com,B.F.A","Post_Status":1,"Eventdate":"21/06/2016","JobsOrInternships":null}

如何显示它。

4 个答案:

答案 0 :(得分:0)

public class ArrayBean {
    public String Name;
    public String PositionName;
    public String DateOfEvent;
    public String EvetnId;
    public String HetId;
    public String EventDate;
    public String COMP_REQ_ID;
    public String COMP_REQ_TYPE;
    public String JobTitle;
    public String CompanyName;
    public String ReqQualification;
    public String DegreeName;
    public String Eventdate;
    public String JobsOrInternships;
}

 ArrayBean bean;
 JSONArray array=new JSONArray("StringData ");
 JSONObject json;
 for(int i=0;i<array.length();i++){
     bean=new ArrayBean();
     json=new JSONObject();
     json=array.getJSONObject(i);
     bean.COMP_REQ_ID=json.getString("COMP_REQ_ID");
     bean.COMP_REQ_TYPE=json.getString("COMP_REQ_TYPE");
     bean.CompanyName=json.getString("CompanyName");
     bean.DateOfEvent=json.getString("DateOfEvent");
     bean.EventDate=json.getString("EventDate");
     bean.EvetnId=json.getString("EvetnId");
     bean.HetId=json.getString("HetId");
     bean.JobsOrInternships=json.getString("JobsOrInternships");
     bean.JobTitle=json.getString("JobTitle");
     bean.Name=json.getString("Name");
     bean.PositionName=json.getString("PositionName");
     bean.ReqQualification=json.getString("ReqQualification");

 }

答案 1 :(得分:0)

您应该使用JSONArray来解析结果

JSONArray rootArray = new JSONArray(jsonString);
int len = rootArray.length();
for(int i = 0; i < len; ++i) {
    JSONObject json = rootArray.getJSONObject(i);
    String COMP_REQ_ID = json.getString("COMP_REQ_ID");
}

或者您可以使用GSon库为您解析结果。我建议以post为例,如何从ASP.NET Web API查询数据。

我认为你应该明白你的问题,因为我不确定你是否在获取数据或向ui显示数据时遇到问题。

答案 2 :(得分:0)

注意,关注JSON中的数据类型,例如key COMP_REQ_ID的值data-type是int。您应该使用optString or optInt代替getString or getInt来解析结果

JSONArray rootArray = new JSONArray(jsonString);
int len = rootArray.length();
for(int i = 0; i < len; ++i) {
    JSONObject json = rootArray.optJSONObject(i);
    int COMP_REQ_ID = json.optInt("COMP_REQ_ID");
    String COMP_REQ_TYPE = json.optString("COMP_REQ_TYPE");
    ...
}

答案 3 :(得分:0)

我使用了这段代码

    JSONArray rootArray = new JSONArray(StringData);
                    int len = rootArray.length();
                    for (int i = 0; i < len; ++i) {
                        JSONObject json = rootArray.optJSONObject(i);
                        int COMP_REQ_ID = json.optInt("COMP_REQ_ID");
                        String COMP_REQ_TYPE = json.optString("COMP_REQ_TYPE");
                        String CompanyName = json.getString("CompanyName ");
                        String Name = json.getString("Name");
                        String PositionName = json.getString("PositionName");
                        myAwesomeTextview.setText("COMP_REQ_ID:" + COMP_REQ_ID + "\n" + "COMP_REQ_TYPE" + COMP_REQ_TYPE + "\n" + "CompanyName" + CompanyName + "\n" + "Name" + Name + "\n" + "PostionNmae" + PositionName);
                    }

但是后来结果还没有在我的手机上显示而在android studio logcat它显示了我:

   reporting:java.lang.NullPointerException at   com.example.anand.Next$1.callback(Next.java:55)
                                                                 at com.example.anand.Next$1.callback(Next.java:24)
                                                                 at