只需从JSON字符串预览列表视图

时间:2017-11-24 10:55:38

标签: java android json

我从SOAP中提取了JSON字符串 我想在ListView中显示它 需要添加什么代码请建议他们没有命名的JSON对象

My Mainactivity.java

private static String SOAP_ACTION = "http://tempuri.org/METHOD_NAME";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "METHOD_NAME";
private static String URL = "myurl.com";

Button button1;
TextView textView1;
String jsonStr;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

    button1 =(Button) findViewById(R.id.idbutton1);
    textView1 = (TextView) findViewById(R.id.idtxtView1);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //Use this to add parameters
            request.addProperty("id_no", 123
            request.addProperty("serial_no", 2);
            request.addProperty("Year", 2017);
            request.addProperty("Username", "username");
            request.addProperty("Password", "password");

            //Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                // Get the SoapResult from the envelope body.
                SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
                jsonStr = resultsRequestSOAP.toString();
                textView1.setText(jsonStr);


            } catch(Exception E) {

               xy = ("ERROR:" + E.getClass().getName() + ": " + E.getMessage());

            }
            textView1.setText(jsonStr);

        }
    });

}

我的JSON字符串(来自web)是

[{"name":"ravi","idnumber":2,"surname":"bhosle","areaname":"pune","state":"maharashtra","country":"india","registrationfees":100},{"name":"amit","idnumber":2,"surname":"gayakwad","areaname":"nanded","state":"maharashtra","country":"india","registrationfees":80}]

使用item_list在activity_main中创建的listView 如何在该

中显示该JSON字符串

2 个答案:

答案 0 :(得分:0)

试试这个

         try{    

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            //this is the actual part that will call the webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);

            // Get the SoapResult from the envelope body.
            SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();


            JSONArray jarray = new JSONArray(resultsRequestSOAP );
            JSONObject obj = new JSONObject();
            System.out.println("jsonarraylength-->>"+jarray.length());
            for(int i=0;i<jarray.length();i++){
                obj = jarray.getJSONObject(i);

                System.out.println(obj.getString("name"));
                System.out.println(obj.getString("idnumber"));

            }
        }catch(Exception e){
            e.printStackTrace();
        }

答案 1 :(得分:0)

删除了textView1.setText(jsonStr); 并使用下面的代码

&#13;
&#13;
private static String SOAP_ACTION = "http://tempuri.org/METHOD_NAME";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "METHOD_NAME";
private static String URL = "myurl.com";

Button button1;
TextView textView1;
String jsonStr;

ListView listView1 =(ListView) findViewById(R.id.idlistView1);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

    button1 =(Button) findViewById(R.id.idbutton1);
    textView1 = (TextView) findViewById(R.id.idtxtView1);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //Use this to add parameters
            request.addProperty("id_no", 123
            request.addProperty("serial_no", 2);
            request.addProperty("Year", 2017);
            request.addProperty("Username", "username");
            request.addProperty("Password", "password");

            //Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                // Get the SoapResult from the envelope body.
                SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
                jsonStr = resultsRequestSOAP.toString();
                textView1.setText(jsonStr);


            } catch(Exception E) {

               xy = ("ERROR:" + E.getClass().getName() + ": " + E.getMessage());

            }
            if (xy != null) {
                    try {
                        // JSONObject jsonObj = new JSONObject(xy);

                        // Getting JSON Array node
                        JSONArray contacts = new JSONArray(xy);

                        // looping through All Contacts
                        for (int i = 0; i < contacts.length(); i++) {
                            JSONObject c = contacts.getJSONObject(i);

                            String sroname = c.getString("name");
                            String docnumber = c.getString("idnumber");
                            String ariclename = c.getString("surname");
                            String areaname = c.getString("areaname");
                            String consideration_amt = c.getString("state");
                            String marketvalue = c.getString("country");
                            String stampdutypaid = c.getString("registrationfees");

                            // Phone node is JSON Object

                            // tmp hash map for single contact
                            HashMap<String, String> contact = new HashMap<>();

                            // adding each child node to HashMap key => value
                            contact.put("name","Name:   "+ name);
                            contact.put("idnumber","Id Number:   "+ idnumber);
                            contact.put("surname","Surname:   "+ surname);
                            contact.put("areaname","Area Name:   "+ areaname);
                            contact.put("state","State:   "+ state);
                            contact.put("country","Country:   "+ country);
                            contact.put("registrationfees","Registration Fees:   "+ registrationfees);

                            // adding contact to contact list
                            docdetails.add(contact);
                        }
                    }
                    catch(JSONException e)
                    {
                        String error = ("Json parsing error: "  + e.getMessage());
                        Toast.makeText(getApplicationContext(),error,Toast.LENGTH_LONG).show();

                    }
                } else
                {
                    String error = ("Couldn't get json from server. ");
                    Toast.makeText(getApplicationContext(),error,Toast.LENGTH_SHORT).show();
                }
                /**
                 * Updating parsed JSON data into ListView
                 * */

                ListAdapter adapter = new SimpleAdapter(WebResponce.this, docdetails,R.layout.list_item, new String[]{"name","idnumber","surname","areaname","state","country","registrationfees"},
                        new int[]{R.id.idname,R.id.ididnumber,R.id.idsurname, R.id.idareaname,R.id.idstate,R.id.idcountry,R.id.idregistrationfees});
                listView11.setAdapter(adapter);

        }
    });

}
&#13;
&#13;
&#13;

解决