gpt4 book ai didi

json - 当发布到我的 Django API : errorcom. google.gson.JsonParseException: 无法解析 json

转载 作者:行者123 更新时间:2023-12-04 01:08:11 24 4
gpt4 key购买 nike

有人可以告诉我我在这篇文章中做错了什么吗?

我怀疑我错误地发布到我的 django API。我基本上有一个 Question 对象,其中包含一个答案数组的字段。我可以在没有答案的情况下正确发布,但是当我尝试添加 JsonArray 以获取答案时,发布失败并出现无法解析错误。

LogCat 摘录

05-21 00:12:52.875  15720-15720/com.pipit.waffle D/ConnectToBackend﹕ {"text":"gf or ed","answers":[{"text":"gf","votes":0,"id":null},{"text":"ed","votes":0,"id":null}],"user_id":"temp user id"}
05-21 00:12:52.875 15720-15720/com.pipit.waffle D/ConnectToBackend﹕ postQuestion called with {MY API} and has errorcom.google.gson.JsonParseException: unable to parse json
05-21 00:12:52.875 15720-15720/com.pipit.waffle D/ConnectToBackend﹕ postQuestion returns result with NULL

Django 边

序列化程序.py
class EmbeddedAnswerSerializer(serializers.ModelSerializer):
votes = serializers.IntegerField(read_only=True)
picture = serializers.ImageField(read_only=True)
class Meta:
model = Answer
fields = ('id', 'picture', 'text', 'votes',)


class QuestionSerializer(serializers.ModelSerializer):
answers = EmbeddedAnswerSerializer(many=True, source='answer_set')

class Meta:
model = Question
fields = ('id', 'answers', 'created_at', 'text', 'user_id',)


class AnswerSerializer(serializers.ModelSerializer):
text = serializers.CharField(read_only=True)
vote = serializers.BooleanField(required=True)
picture = serializers.ImageField(read_only=True)
votes = serializers.IntegerField(read_only=True)

模型.py
class Question(models.Model):
user_id = models.CharField(max_length=36)
text = models.CharField(max_length=140)
created_at = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return u'Question #{}'.format(self.pk)

class Answer(models.Model):
picture = models.ImageField(("Picture"), upload_to=upload_pic_to, blank=True)
question = models.ForeignKey(Question)
text = models.CharField(max_length=25)
votes = models.IntegerField(default=0)

def __unicode__(self):
return u'Answer to question {} ({} votes)'.format(self.question_id, self.votes)

客户端
(安卓)
public static void postQuestion(final Context mcontext, Question mquestion){
JsonArray answerarray = new JsonArray();
JsonObject answerjson = new JsonObject();
JsonObject answerjson2 = new JsonObject();
answerjson.addProperty("text", mquestion.getChoices().get(0).getAnswerBody());
answerjson2.addProperty("text", mquestion.getChoices().get(1).getAnswerBody());
answerjson.addProperty("votes", 0);
answerjson2.addProperty("votes", 0);
answerjson.addProperty("id", mquestion.getId());
answerjson2.addProperty("id", mquestion.getId());
answerarray.add(answerjson);
answerarray.add(answerjson2);

JsonObject json = new JsonObject();
json.addProperty("text", mquestion.getQuestionBody());
json.add("answers", answerarray);
json.addProperty("user_id", "temp user id");
final String url = "my endpoint";
Ion.with(mcontext) //Ion Koush is just a library for making async android requests to a URL - doubt this is the problem
.load(url)
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null){
Log.d("ConnectToBackend", "postQuestion called with "+url+" and has error" + e.toString());
if (result==null){
Log.d("ConnectToBackend", "postQuestion returns result with NULL");
}
}
else{
//Do Stuff
}

}
});
}

这是一个成功 GET 到同一端点的示例
{"id":5,"answers":[{"id":10,"picture":"someurl","text":"microsoft","votes":0},{"id":9,"picture":"someurl","text":"apple","votes":0}],"created_at":"2015-03-15T04:14:00.782903Z","text":"MicroSoft or Apple","user_id":"8"}

最佳答案

您确定它实际上不是成功的字符串而不是字典吗?我在Django方面遇到过这个问题,对Android不太了解。

使用python它将是:

import json
your_dict = {...}

# this stringifies the dict
json.dumps(your_dict)

我知道您正在客户端上执行此操作,因此上面的代码不会成为您的答案,但这是一个想法,希望对您有所帮助!

关于json - 当发布到我的 Django API : errorcom. google.gson.JsonParseException: 无法解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com