【问题标题】:I want to Implement this code with @SerializedName GSON object我想用@SerializedName GSON 对象来实现这段代码
【发布时间】:2016-04-06 10:23:19
【问题描述】:

想要实现@SerializedName Gson 我不知道如何实现 Gson 请帮我如何实现@SerializedName GSON

public class ModelClass {

@SerializedName("longi")
public String longitudeServer;

@SerializedName("lati")
public String latitudeServer;

@SerializedName("uniqueid")
public String uniqueidSserver;

public ModelClass(){
    // blank constructor is required
}

public String getLongitude(){
    return longitudeServer;
}


public String getLatitude(){
    return latitudeServer;
}


public String getUniqueId(){
    return uniqueidSserver;
}


//... More setter and getter here

}

【问题讨论】:

    标签: android performance android-layout listview android-fragments


    【解决方案1】:

    1:-问题是跳转当前指针。

    在公共类 MainActivity 中创建方法 addMarker() 扩展 AppCompatActivity

    private void addMarker() {
        try {
            //ArrayList list = new ArrayList<>();
            //TODO: remove previus markers
            if (marker != null) {
                mMap.clear();
                Toast.makeText(getApplicationContext(), "Remove", Toast.LENGTH_LONG).show();
    
            }
            for (LatLng object : latLngList)
    
                marker = mMap.addMarker(new MarkerOptions().title("User Name").position(object).icon(BitmapDescriptorFactory.fromResource(R.drawable.female4)));
    
            System.out.println(marker.getPosition() + "  Marker position.......");
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Error ", Toast.LENGTH_LONG).show();
            // mMap.clear();
        }
    }
    

    然后调用它

     if (servicesOK()) {
            setContentView(R.layout.activity_map);
    
            if (initMap()) {
                //  gotoLocation(SEATTLE_LAT, SEATTLE_LNG, 15);
    
                mLocationClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();
    
                mLocationClient.connect();
    
    
                mMap.setMyLocationEnabled(true);
            } else {
                Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show();
            }
    
            addMarker();
    
        } else {
            setContentView(R.layout.activity_main);
        }
    

    在 OnPostExecute 中

     protected void onPostExecute(Boolean result) {
            // dialog.cancel();
            // adapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Receicve data from server", Toast.LENGTH_LONG).show();
    
            if (result == false) {
                Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
    
            }
    //            try {
    //                //ArrayList list = new ArrayList<>();
    //                //TODO: remove previus markers
    //                if (marker != null) {
    //                    mMap.clear();
    //                    Toast.makeText(getApplicationContext(), "Remove",     Toast.LENGTH_LONG).show();
    //
    //                }
    //                for (LatLng object : latLngList)
    //
    //                    marker = mMap.addMarker(new MarkerOptions().title("User Name").position(object).icon(BitmapDescriptorFactory.fromResource(R.drawable.female4)));
    //
    //                System.out.println(marker.getPosition() + "  Marker position.......");
    //            } catch (Exception e) {
    //                Toast.makeText(MainActivity.this, "Error ", Toast.LENGTH_LONG).show();
    //                // mMap.clear();
    //            }
    
        }
    
    }
    

    【讨论】:

    【解决方案2】:

    SerializedName 的虚拟使用

        @SerializedName("Savings")
        private String savings;
    

    你需要添加

    编译'com.google.code.gson:gson:2.3.1'

    build.gradle 中的内部依赖项

    【讨论】:

    • 它在哪里使用? @SerializedName("Savings") 私有字符串节省;
    • 在这一行收到错误 ModelClass s = LoganSquare.parse(jsonarray.getJSONObject(i).toString(), ModelClass.class);
    • 公共类 ModelClass { @SerializedName("longi") public String longitudeServer; @SerializedName("lati") public String latitudeServer; @SerializedName("uniqueid") public String uniqueidSserver; public ModelClass(){ // 需要空白构造函数 } public String getLongitude(){ return longitudeServer; } 公共字符串 getLatitude(){ 返回纬度服务器; } public String getUniqueId(){ return uniqueidSserver; } //... 更多的 setter 和 getter }
    • 我如何在 DoInBacground 中实现?
    • 当您收到响应时,您可以使用ModelClass modelClass = new Gson().fromJson(response, ModelClass.class); 解析它,这将解析响应并将数据填充到您的模型类对象中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2016-04-17
    • 2018-02-25
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多