有人能告诉我这篇文章中我做错了什么吗?
我怀疑我错误地发布到我的django API。我基本上有一个 Question 对象,它有一个 Answers 数组的字段。我可以在没有答案的情况下正确发布,但是当我尝试为答案添加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 Side
serializers.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)
models.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"}
答案 0 :(得分:2)
你确定它实际上不是一个成功的字符串而不是字典吗?我在Django方面遇到过这个问题,对Android并不了解。
使用python,它将是:
import json
your_dict = {...}
# this stringifies the dict
json.dumps(your_dict)
我知道你在客户端上这样做,所以上面的代码不是你的答案,但这是一个想法,希望它有所帮助!
答案 1 :(得分:2)
您可以通过更改QuestionSerializer以另一种方式尝试此操作:
getSource()
答案 2 :(得分:2)
我认为该服务的数据格式不正确......
{"text":"gf or ed","answers":[{"text":"gf","votes":0,"id":null},{"text":"ed","votes":0,"id":null}],"user_id":"temp user id"}
'图片'不存在,你不是从Android发送它...而且Django默认情况下“需要”字段,所以它应该导致你的问题......
你有没有来自python的痕迹?尝试调试它。
答案 3 :(得分:1)
你在这里缺少你的views.py代码,但我猜你正在使用ListCreateAPIView开箱即用。事实证明,django rest框架不支持自动创建开箱即用的嵌套对象,因此您需要添加自己的代码来解决该功能。这里有很好的文档:
http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations