如何实现jsonArray扩展按钮数组?

时间:2017-05-12 06:44:43

标签: java android arrays json soap

我有一个酒店类扩展为按钮,其中包含一些值。我在下面分享。

import android.content.Context;
import android.widget.Button;

public class Hotel extends Button{
    String  id;
    String name;
    String company_id;

    public Hotel(Context context, String id, String name, String company_id) {
        super(context);
        this.id = id;
        this.name = name;
        this.company_id = company_id;
    }
}

我有另一个类来调用soap web服务,它将酒店作为json字符串返回。 我想把json hotel数组作为一个酒店数组并将其作为数组返回。但是,当我运行应用程序时,它表示"您的应用程序停止了#34;。我怎样才能解决这个问题?我认为它与Context有关。我正在共享服务类和我调用getHotels方法的类。

ServiceManager类我使用soap和jsonArray

import android.content.Context;    
import org.json.JSONArray;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;    
    public class ServiceManager {
        private static final String METHOD_NAME = "GetHotels";
        private static final String NAMESPACE = "http://tempuri.org/";
        private static final String SOAP_ACTION = "http://tempuri.org/GetHotels";
        private static final String URL = "http://192.168.1.170/HotelApps_WebServices.asmx";

        SoapObject soapObject;
        SoapSerializationEnvelope soapSerializationEnvelope;
        HttpTransportSE httpTransportSE;

        public static Context mContext;
        public ServiceManager(Context context){
            this.mContext = context;
        }

        public Hotel[] GetHotels(int Company_id) {

            soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
            soapObject.addProperty("Company_id", Company_id);

            soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapSerializationEnvelope.dotNet = true;
            soapSerializationEnvelope.setOutputSoapObject(soapObject);

            httpTransportSE = new HttpTransportSE(URL);
            httpTransportSE.debug = true;

            Hotel hotels[]=new Hotel[6];
            try {
                httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
                SoapPrimitive soapPrimitive=(SoapPrimitive)soapSerializationEnvelope.getResponse();
                JSONArray jsonHotels = new JSONArray(soapPrimitive.toString());

                for (int i=0; i<6; i++) {
                    hotels[i]=new Hotel( mContext, jsonHotels.getJSONObject(i).getString("Hotel_id"),jsonHotels.getJSONObject(i).getString("Hotel_Name"), jsonHotels.getJSONObject(i).getString("Company_id"));

                }
                return hotels;
            } catch (Exception ex) {
                ex.printStackTrace();
                return hotels;
            }
        }

    }

这就是我调用getHotels方法的地方

package com.example.arzucaki.sherwoodhotels;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridLayout;

public class Hotels extends AppCompatActivity {

    Hotel hotels[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotels);
        GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels);

        Thread getHotels=new Thread(new Runnable() {
            @Override
            public void run() {
                ServiceManager service=new ServiceManager(getApplicationContext());
                hotels=service.GetHotels(1);
            }
        });
        getHotels.start();
        for (int i=0; i<hotels.length; i++) {
            glHotels.addView(hotels[i]);
        }
    }
}

这是logcat异常

  

E / AndroidRuntime:致命异常:主要                                                                                      过程:com.example.arzucaki.sherwoodhotels,PID:5656                                                                                      java.lang.RuntimeException:无法启动活动

2 个答案:

答案 0 :(得分:1)

我得到了ArrayIndexOutOfBound异常。像这样重新计算你的代码。

LogFormat "{ \"time\":\"%{%Y-%m-%d}tT%{%T}t.%{msec_frac}tZ\", \"process\":\"%D\", \"filename\":\"%f\", \"remoteIP\":\"%a\", \"host\":\"%V\", \"request\":\"%U\", \"query\":\"%q\", \"method\":\"%m\", \"status\":\"%>s\", \"userAgent\":\"%{User-agent}i\", \"referer\":\"%{Referer}i\" }," combined

但您可以使用AsyncTask更好地处理UI。

答案 1 :(得分:0)

这是问题,因为您的数组hotels []未初始化并且您尝试访问for循环。所以它抛出了nullpointer异常。

您应该使用AsyncTask而不是Thread,修改代码如:

   public class Hotels extends AppCompatActivity {

        Hotel hotels[];

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_hotels);
            GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels);
            new HotelInfo().execute();

        }

        private class HotelInfo extends AsyncTask<Void,Void,Void>{

                @Override
                protected Void doInBackground(Void... params) {
                    ServiceManager service=new ServiceManager(getApplicationContext());
                    hotels=service.GetHotels(1);
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    if(hotels != null){
                        for (int i=0; i<hotels.length; i++) {
                            glHotels.addView(hotels[i]);
                        }
                    }
                }
            }
}

希望对你有所帮助!