Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用gson在Android中转换JSON对象_Java_Android_Wcf - Fatal编程技术网

Java 使用gson在Android中转换JSON对象

Java 使用gson在Android中转换JSON对象,java,android,wcf,Java,Android,Wcf,我不熟悉Java和Android,所以请耐心听我说。我试图创建一个调用wcf服务的函数,并将返回的结果从JSON转换为Java对象(我将类型作为对象t),但它在t上抛出空指针异常,它必须是null,因为我只想传递一个正确类型的对象,以便在转换时填充它。请帮帮我 public static String Post(String serviceURL, Map<String, String> entites, Class<?> t) { Str

我不熟悉Java和Android,所以请耐心听我说。我试图创建一个调用wcf服务的函数,并将返回的结果从JSON转换为Java对象(我将类型作为对象
t
),但它在
t
上抛出空指针异常,它必须是
null
,因为我只想传递一个正确类型的对象,以便在转换时填充它。请帮帮我

    public static String Post(String serviceURL, Map<String, String> entites,
        Class<?> t) {
    String responseString = "";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);


            Gson gson = new GsonBuilder().create();
            //now we will try to convert the class to the specified type.
            t = (Class<?>) gson.fromJson(responseString, t);



    } catch (Exception e) {

        responseString = e.toString();

    }
    return responseString;
publicstaticstringpost(字符串服务URL、映射实体、,
(t类){
字符串responseString=“”;
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
Gson Gson=new GsonBuilder().create();
//现在,我们将尝试将该类转换为指定的类型。
t=(Class)gson.fromJson(responseString,t);
}捕获(例外e){
responseString=e.toString();
}
回报率;
非常感谢

经过一些尝试,我最终得到了这段代码,但仍然面临空指针异常

    MemberInfo mem = new MemberInfo();

    TypeToken<MemberInfo> m = null ;
    ServiceCaller.Post(getString(R.string.LoginService), values , m);


            Gson gson = new GsonBuilder().create();
            //now we will try to convert the class to the specified type.
            t = (TypeToken<T>) gson.fromJson(responseString, (Type) t);
MemberInfo mem=newmemberinfo();
typetokenm=null;
ServiceCaller.Post(getString(R.string.LoginService),值,m);
Gson Gson=new GsonBuilder().create();
//现在,我们将尝试将该类转换为指定的类型。
t=(TypeToken)gson.fromJson(responseString,(Type)t);

据我所知,这是不可能的。为了做到这一点,gson必须只能从序列化字符串中检测正确的类。但是,单个字符串可以用许多有效的方式进行解释。例如,以gson网站为例:

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  BagOfPrimitives() {
    // no-args constructor
  }
}
在此类的实例上使用
Gson.toJson
会产生以下字符串:

{"value1":1,"value2":"abc"}
但是,如果我制作的另一个类与第一个类在所有方面相同,但名称不同:

class SackOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  SackOfPrimitives() {
    // no-args constructor
  }
}
然后还将序列化为相同的字符串:

{"value1":1,"value2":"abc"}

我的观点是,给定一个像
{“value1”:1,“value2”:“abc”}
这样的字符串,gson无法确定是否应该将其反序列化为
BagofPrimities
类型或
SakofPrimities
类型的对象。因此,您必须始终为gson提供正确的类型,因为gson不可能自己找到它。

当您说它工作不正常时,您的确切意思是什么?编写的代码抛出一个异常,t为null,必须作为null传递。我只想传递类型,这就是我想要的。所以你的意思是这不是运行时错误,而是语法错误!这与GSson或android无关。。。从代码的外观来看,Post方法缺少一个紧括号。我刚刚解决了这个问题,对不起,我复制了错误的代码。。t为null是一个运行时异常。您需要显示stacktrace(来自logcat的输出)以准确显示您的意思。