我正在尝试创建一个 Web 服务并从 Android 中使用它。它正在使用 GET 方法,但我无法调用 POST 方法。
@Path("/dictionary")
public class DictionaryWebService {
private final static String ID = "id";
private final static String WORD = "palabra";
private final static String DESCRIPTION = "description";
.....
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Word postNewWord(MultivaluedMap<String, String> newWord) {
Word dict;
String id = newWord.getFirst(ID);
String word = newWord.getFirst(WORD);
String description = newWord.getFirst(DESCRIPTION);
dict = new Word(id, word, description);
dictionary.putNewWord(dict);
System.out.println("New Word info: " + dict.toString());
return dict;
}
}
我的安卓代码是:
@Override
public void onClick(View arg0) {
System.out.println("onClick!!!");
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2:8080/DictionaryWebService/rest/dictionary/");
post.setHeader("content-type", "application/json");
//Construimos el objeto cliente en formato JSON
JSONObject dato = new JSONObject();
try {
dato.put("id", txtId.getText().toString());
dato.put("palabra", txtWord.getText().toString());
dato.put("description", txtDescription.getText().toString());
StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
System.out.println("OKAY!");
.....
}
});
我不知道,但我无法执行网络服务,,,它没有被调用。怎么了??有人可以帮助我吗?onclick 方法完成,没有任何错误。
谢谢你。