Может кто-нибудь сказать мне, что я делаю неправильно в этом посте?
Я подозреваю, что неправильно переношу на свой 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
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"}