/ जावा प्रकार के लिए एक संदेश बॉडी रीडर, class org.json.JSONObject… और MIME मीडिया प्रकार, एप्लिकेशन / json नहीं मिला - java, android, json, web-services, rest

जावा प्रकार, वर्ग org.json.JSONObject… और MIME मीडिया प्रकार, एप्लिकेशन / json के लिए एक संदेश बॉडी रीडर नहीं मिला - java, android, json, web-services, rest

मैं एंड्रॉइड से जर्सी आराम करने वाली वेब सेवा को कॉल करने का प्रयास कर रहा हूं। मेरा Android कोड है

क्लाइंट कोड:

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://X.X.X.X:8080/RestfulService/rest/post");
post.setHeader("content-type", "application/json");

JSONObject dato = new JSONObject();
dato.put("email", email);
dato.put("password", password);

StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String rs = EntityUtils.toString(resp.getEntity());
return rs

Webservice कोड

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public String AuthMySQL(JSONObject json) {

String password = (String) json.get("password");
String email = (String) json.get("email");

*I am using the string values to get the result from the database*

}

त्रुटि मुझे कुछ इस तरह है com.sun.jersey.api.client.ClientHandlerException: जावा प्रकार, वर्ग org.json.JSONObject .... और MIME मीडिया प्रकार, एप्लिकेशन / json के लिए एक संदेश बॉडी रीडर नहीं मिला।

आपका सहयोग सराहनीय है

उत्तर:

जवाब के लिए 0 № 1

यह तब होता है जब आपके पास सही लाइब्रेरी को POJO को मैप करने के लिए शामिल सही लाइब्रेरी नहीं होती है या इनपुट के लिए उपयुक्त POJO नहीं होता है।

जोड़कर देखिए जर्सी-जसन मावेन निर्भरता आपकी परियोजना के लिए


जवाब के लिए 0 № 2

यदि आप "लाइब्रेरी जोड़ना नहीं चाहते हैं, और बस पार्स किए गए JSON (यानी POJO में मैप किए बिना) प्राप्त करना चाहते हैं, तो आप बस एक बुनियादी लागू कर सकते हैं MessageBodyReader, उदाहरण:

public class JSONObjectMessageBodyReader implements MessageBodyReader<JSONObject> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JSONObject.class && mediaType.equals(MediaType.APPLICATION_JSON_TYPE);
}

@Override
public JSONObject readFrom(Class<JSONObject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
// Using Apache Commons IO:
String body = IOUtils.toString(entityStream, "utf-8");
return new JSONObject(body);
} catch(JSONException e) {
throw new BadRequestException("Invalid JSON", e);
}
}
}

फिर आपके webservice कोड में:

@POST
public Response doSomething(JSONObject body) {
...
}