【发布时间】:2016-04-05 02:53:18
【问题描述】:
我正在 Android Studio 中创建一个应用程序。我正在尝试通过我之前配置的休息服务将 5 个参数发送到数据库。我已经通过浏览器插件和发布请求测试了网页并且工作正常,所以我确定问题出在应用程序本身。我还确保我发送的数据也很好(locationX、locationY、时间、日期、用户名)。有谁知道为什么这不起作用?
public void onClickSendCoordinates(View v)
{
final EditText textbox3 = (EditText) findViewById(R.id.textbox3);
String date = DateFormat.getDateInstance().format(new Date());
String time = DateFormat.getTimeInstance().format(new Date());
String url = "http://somewebsite.com/index.php";
String locationX = textbox2.getText().toString();
String locationY = textbox1.getText().toString();
String username = "user_1";
EditText textbox4 = (EditText) findViewById(R.id.textbox4);
textbox4.setText(username + locationX + locationY + date + time);
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", username);
params.put("locationX", locationX);
params.put("locationY", locationY);
params.put("date", date);
params.put("time", time);
httpClient.post(url, params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String res) {
//called when response HTTP status is "200 OK"
textbox3.setText("Success");
// Toast.makeText(getApplication(),"Sending URL " + url + "with params " + params.toString(), Toast.LENGTH_LONG);
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
textbox3.setText("Failure");
// Toast.makeText(getApplication(), "Sending URL " + url + "with params " + params.toString(), Toast.LENGTH_LONG);
//called when response HTTP status is "4xx" (eg. 401, 403, 404)
}
});
}
【问题讨论】:
-
确切的问题在哪里?您的服务器是否收到呼叫?还是通话本身没有发生?
-
HttpClient 已弃用,请使用 HttpUrlConnection。
标签: android android-asynctask androidhttpclient android-async-http