如何使用Gson

时间:2017-03-25 07:19:50

标签: android json gson

public class MainActivity extends AppCompatActivity {


TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    textView = (TextView) findViewById(R.id.textView1);



    FloatingActionButton fab = (FloatingActionButton) 
findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//I am getting error on this line becuse ACC_id variable is double and the data coming from server might have some blank value so it is giving me exception  
            AccountDetailsList list = new Gson().fromJson(getData(), AccountDetailsList.class);
                    Log.d("MAIN_ACTIVITY", list.getAccountDetails().size() + " ");
                    for (AccountDetails accountDetails : list.getAccountDetails()) {
                        Log.d("MAIN_ACTIVITY", accountDetails.getCUSTOMER_NAME());
                    }     
});
// this is the sample json data which is coming from server
public String getData() {
    return "{\"ACCOUNT_DETAILS\":[{\"ACC_ID\":\"\",\"ACC_NUMBER\":\"\",\"CUSTOMER_NAME\":\"\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":1,\"ACC_NUMBER\":12345,\"CUSTOMER_NAME\":\"Cheenu\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":2,\"ACC_NUMBER\":67890,\"CUSTOMER_NAME\":\"Dhruv\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":3,\"ACC_NUMBER\":13467,\"CUSTOMER_NAME\":\"Chetan\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":4,\"ACC_NUMBER\":16789,\"CUSTOMER_NAME\":\"Darsh\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":5,\"ACC_NUMBER\":98764,\"CUSTOMER_NAME\":\"Arsh\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":6,\"ACC_NUMBER\":130500,\"CUSTOMER_NAME\":\"Madhur\",\"CONTACT_NO\":\"\"}]}";
}}

当我解析JSON数据时,我得到以下异常 线:     例外

  

致命的例外:主要                       处理:tfs.anonestep.com.tfs,PID:26604                                                                          java.lang.NumberFormatException:无效的double:“”在java.lang.StringToReal.invalidReal(StringToReal.java:63)                                                                              在java.lang.StringToReal.parseDouble(StringToReal.java:267)                                                                              在java.lang.Double.parseDouble(Double.java:301)                                                                              在com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:925)                                                                              在com.google.gson.Gson $ 3.read(Gson.java:260)                                                                              在com.google.gson.Gson $ 3.read(Gson.java:254)                                                                              在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:117)                                                                              在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:217)                                                                              在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)                                                                              在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:81)                                                                              在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:60)                                                                              在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:117)                                                                              在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:217)                                                                              在com.google.gson.Gson.fromJson(Gson.java:814)                                                                              在com.google.gson.Gson.fromJson(Gson.java:779)                                                                              在com.google.gson.Gson.fromJson(Gson.java:728)                                                                              在com.google.gson.Gson.fromJson(Gson.java:700)                                                                              at tfs.anonestep.com.tfs.MainActivity $ 1.onResponse(MainActivity.java:49)                                                                              at tfs.anonestep.com.tfs.MainActivity $ 1.onResponse(MainActivity.java:42)                                                                              在com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)                                                                              在com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)                                                                              在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)                                                                              在android.os.Handler.handleCallback(Handler.java:739)                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                              在android.os.Looper.loop(Looper.java:148)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5539)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

模型类

    public class AccountDetailsList {
@SerializedName("ACCOUNT_DETAILS")
ArrayList<AccountDetails> accountDetails;

public ArrayList<AccountDetails> getAccountDetails() {
    return accountDetails;
}

public void setAccountDetails(ArrayList<AccountDetails> accountDetails) {
    this.accountDetails = accountDetails;
}
//Model class
}


public class AccountDetails {

double ACC_ID;
String CUSTOMER_NAME;
String ACC_NUMBER;
String CONTACT_NO;
}

0 个答案:

没有答案