我正在Android Studio中创建一个应用程序。我正试图通过我之前配置的rest服务向数据库发送5个参数。我已经通过浏览器插件和帖子请求测试了网页,并且运行良好,所以我确信问题来自应用程序本身。我还确保我发送的数据也很好(位置X、位置Y、时间、日期、用户名)。有人知道为什么不起作用吗?
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在23中被强烈反对。请使用HttpURLConnconnection。
// HTTP GET request
public String sendGet(String apiUrl) {
StringBuilder result = new StringBuilder();
HttpURLConnection urlConnection = null;
try {
URL url = new URL(apiUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return result.toString();
}
对于HTTP POST
// HTTP POST request
public String sendPost(String apiurl, String args) {
String response = "";
try {
URL url= new URL(apiurl);;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DataOutputStream printout;
DataInputStream input;
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
printout = new DataOutputStream(conn.getOutputStream());
byte[] data=args.getBytes("UTF-8");
printout.write(data);
printout.flush();
printout.close();
int responseCode =conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}