【发布时间】:2014-08-12 01:17:47
【问题描述】:
我使用来自 http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/ 的 Slim 框架构建 web 服务
我检查了 chrome 的 API,一切正常。但我不知道我应该如何在 android 应用程序上使用它。 我尝试这样做:
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(MY_API_URL);
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("name", "Name_"));
nameValuePair.add(new BasicNameValuePair("email", "email@email.com"));
nameValuePair.add(new BasicNameValuePair("password", "pass"));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
怎么了?
【问题讨论】: