字符串未返回

时间:2017-11-30 05:43:10

标签: java android string

我有创建函数来返回字符串,该字符串从模型类附加了许多其他字符串。请参阅下面的代码

public static String getDefaultParameters(Context context){
        System.out.println("Default Params");

        PreDefinedAttributes preDefinedAttributes = new PreDefinedAttributes(context);


        StringBuffer sb = new StringBuffer();
        sb.append("&_vpw="+preDefinedAttributes.getWidth());
        sb.append("&_vph="+preDefinedAttributes.getHeight());
        sb.append("&sdk_version="+preDefinedAttributes.getSdk_version());
        sb.append("&_src=android-sdk"
                +"&_os="+preDefinedAttributes.getMobile_os());
        sb.append("&os_version="+preDefinedAttributes.getMobile_version());
        sb.append("&manufacturer="+preDefinedAttributes.getMobile_manufacturer());

        System.out.println("Builder Params "+sb.toString());

        return  sb.toString();
    }

但是我没有在日志中获得Builder Params值。我已经使用了StringBuilder和带有+符号的普通String,没有任何效果。

Code ForPredefined Attributes Class,其中我填充了param数据并返回值。

public class PreDefinedAttributes {
private Context mContext;
private String mobile_os,mobile_model,mobile_brand,mobile_version,mobile_manufacturer;
private String sdk_version,src,appname,appversion;
private String lat="",lng="",device_id;
private static final int REQUEST_FINE_LOCATION=0;
private int height,width;


public PreDefinedAttributes(Context context) {
mContext = context;

    this.mobile_model = android.os.Build.MODEL;
    this.mobile_os = "Android";
    this.mobile_brand = Build.BRAND + " "+Build.PRODUCT;
    this.mobile_version = Build.VERSION.RELEASE;
    this.mobile_manufacturer = Build.MANUFACTURER;

    this.sdk_version = Utils.SDK_VERSION;
    this.src = Utils.SDK_SRC;
    this.device_id = Utils.getDeviceId(mContext);
    try {
        this.appversion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
    }catch (Exception e){
        this.appversion = "";
    }

    final PackageManager pm = mContext.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo( mContext.getPackageName(), 0);
    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }
    this.appname = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");





     height = Resources.getSystem().getDisplayMetrics().heightPixels;
     width = Resources.getSystem().getDisplayMetrics().widthPixels;



    //loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);
    try {
        SingleShotLocationProvider.requestSingleUpdate(mContext,
                new SingleShotLocationProvider.LocationCallback() {
                    @Override
                    public void onNewLocationAvailable(SingleShotLocationProvider.GPSCoordinates location) {
                        Log.d("Location", "my location is " + location.toString());
                        lat = ""+location.latitude;
                        lng = ""+location.longitude;
                    }
                });
    }catch (SecurityException e){
        e.printStackTrace();
    }finally {

    }


}

public int getHeight() {
    return height;
}

public int getWidth() {
    return width;
}

public String getDevice_id() {
    return device_id;
}

public String getMobile_brand() {
    return mobile_brand;
}


public String getMobile_model() {
    return mobile_model;
}

public String getMobile_os() {
    return mobile_os;
}

public String getMobile_version() {
    return mobile_version;
}

public String getMobile_manufacturer() {
    return mobile_manufacturer;
}

public String getSdk_version() {
    return sdk_version;
}

public String getSrc() {
    return src;
}

public String getAppname() {
    return appname;
}

public String getAppversion() {
    return appversion;
}

public String getLat() {
    return lat;
}

public String getLng() {
    return lng;
}

}

2 个答案:

答案 0 :(得分:3)

  

您的 String result = ""; 为空,请检查您是否返回空字符串更改

你应该

return sb.toString();

而不是

return result;

修改

public class PreDefinedAttributes {
    private Context mContext;
    private String mobile_os, mobile_model, mobile_brand, mobile_version, mobile_manufacturer;
    private String sdk_version, src, appname, appversion;
    private String lat = "", lng = "", device_id;
    private static final int REQUEST_FINE_LOCATION = 0;
    private int height, width;


    public PreDefinedAttributes(Context context) {
        mContext = context;

        this.mobile_model = android.os.Build.MODEL;
        this.mobile_os = "Android";
        this.mobile_brand = Build.BRAND + " " + Build.PRODUCT;
        this.mobile_version = Build.VERSION.RELEASE;
        this.mobile_manufacturer = Build.MANUFACTURER;

        this.sdk_version = 16 + "";
        this.src = 16 + "";
        this.device_id = 16 + "";
        try {
            this.appversion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (Exception e) {
            this.appversion = "";
        }

        final PackageManager pm = mContext.getPackageManager();
        ApplicationInfo ai;
        try {
            ai = pm.getApplicationInfo(mContext.getPackageName(), 0);
        } catch (final PackageManager.NameNotFoundException e) {
            ai = null;
        }
        this.appname = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");


        height = Resources.getSystem().getDisplayMetrics().heightPixels;
        width = Resources.getSystem().getDisplayMetrics().widthPixels;


        //loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);


    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public String getDevice_id() {
        return device_id;
    }

    public String getMobile_brand() {
        return mobile_brand;
    }


    public String getMobile_model() {
        return mobile_model;
    }

    public String getMobile_os() {
        return mobile_os;
    }

    public String getMobile_version() {
        return mobile_version;
    }

    public String getMobile_manufacturer() {
        return mobile_manufacturer;
    }

    public String getSdk_version() {
        return sdk_version;
    }

    public String getSrc() {
        return src;
    }

    public String getAppname() {
        return appname;
    }

    public String getAppversion() {
        return appversion;
    }

    public String getLat() {
        return lat;
    }

    public String getLng() {
        return lng;
    }
}

获取此类值

 PreDefinedAttributes preDefinedAttributes = new PreDefinedAttributes(this);


        StringBuffer sb = new StringBuffer();
        sb.append("&_vpw=" + preDefinedAttributes.getWidth());
        sb.append("&_vph=" + preDefinedAttributes.getHeight());
        sb.append("&sdk_version=" + preDefinedAttributes.getSdk_version());
        sb.append("&_src=android-sdk"
                + "&_os=" + preDefinedAttributes.getMobile_os());
        sb.append("&os_version=" + preDefinedAttributes.getMobile_version());
        sb.append("&manufacturer=" + preDefinedAttributes.getMobile_manufacturer());

        Log.e("Builder Params " , sb.toString());

<强>输出

enter image description here

答案 1 :(得分:0)

你必须像这样替换你的返回语句

return sb.toString();