I am using Retrofit2 and the problem is that in my onResponse
I get internal server error.
I have made a Call class i.e
public class QuestionCreate {
private String status;
private String result;
private String error;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
My interface is :
public interface MultipartApi {
@Multipart
@POST("webapi/groups/questions/create")
Call<QuestionCreate> createQuestion(@Part("questionText") String questionText, @Part("image") byte[] image, @Part("createdBy") String createdBy, @Part("visibleToMembers") String visibleToMembers, @Part("groupId") String groupId, @Part("questionOptions") String questionOptions, @Part("questionTypeId") String questionTypeId, @Part("isAnswerEditable") String isAnswerEditable);
}
Well just for more detail , this is my small server method of createQuestion:
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public JSONObject createQuestion(@FormDataParam("questionText") String questionText, @FormDataParam("image") InputStream image, @FormDataParam("createdBy") String createdBy, @FormDataParam("visibleToMembers") String visibleToMembers, @FormDataParam("groupId") String groupId, @FormDataParam("questionOptions") String questionOptions, @FormDataParam("questionTypeId") String questionTypeId, @FormDataParam("isAnswerEditable") String isAnswerEditable, @FormDataParam("imageFormat") String imageFormat) {
String message = "";
JSONObject obj = new JSONObject();
Question question = new Question();
System.out.println("Inside createQuestion of class "+this.toString());
try {
question.setQuestionText(questionText); question.setCreatedBy(createdBy);
question.setVisibleToMembers(visibleToMembers);
question.setGroupId(groupId);
question.setQuestionOptions(questionOptions);
question.setQuestionTypeId(questionTypeId);
question.setIsAnswerEditable(isAnswerEditable);
DBConnection.insertQuestion(question, image, servletContext, imageFormat);
message = "Question Created";
obj.put("status", 1);
obj.put("result", message);
return obj;
} catch(SQLException sqle){
System.out.println("createQuestion catch sqle");
message = "A problem occured while creating question : "+sqle.getMessage();
try {
obj.put("status", 501);
obj.put("error", message);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Inside createQuestion catch "+e.getMessage());
message = "A problem occured while creating question : "+e.getMessage();
try {
obj.put("status", 501);
obj.put("error", message);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return obj;
}
}
and i am calling my service like:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URLConstants.URL_BASE+"/")
.addConverterFactory(GsonConverterFactory.create())
.build();
MultipartApi multipartApi = retrofit.create(MultipartApi.class);
Call<QuestionCreate> req = multipartApi.createQuestion(question.getQuestionText(), inputStream, question.getCreatedBy(), question.getVisibleToMembers(), question.getGroupId(), question.getQuestionOptions(), question.getQuestionTypeId(), question.getIsAnswerEditable());
req.enqueue(new Callback<QuestionCreate>() {
@Override
public void onResponse(Call<QuestionCreate> call, Response<QuestionCreate> response) {
String str = response.message();
}
@Override
public void onFailure(Call<QuestionCreate> call, Throwable t) {
//Print Error
}
});
I have just started learning about it yesterday so there must be some mistakes. So I have a few questions:
- How do I just send a map of data (like in StringRequest of volley)?
- How do I get string as a response because I didn't want a model class for this purpose i.e how do I get the just json string?
- What could be the reason for getting internal server error?
Thank you.