gpt4 book ai didi

android - 无法将android中的POST请求发送到Grails后端

转载 作者:行者123 更新时间:2023-12-02 14:11:05 25 4
gpt4 key购买 nike

我在使用Grails和SpringSecurity进行后端发送POST时遇到了一些问题。
我不知道为什么,但是当我发送ROLE_ADMIN方法时,它不起作用。我尝试对用户和密码进行编码,然后将其设置在请求 header 上,并尝试相同但不进行编码。
我真的开始对这个问题感到绝望。
这是我的POST代码:

public String POST2(String targetURL, String urlParameters, String user,
String clave) {
URL url;
String h = "http://";
String u = h+targetURL;

HttpURLConnection connection = null;
try { // Create connection //
// targetURL = URLEncoder.encode(targetURL, "UTF-8");
url = new URL(u);
connection = (HttpURLConnection) url.openConnection(); // cambiarlo
// luego al
// usuario q
String login = user + ":" + clave;
String encoding = new String(org.apache.commons.codec.binary.Base64 //
.encodeBase64(org.apache.commons.codec.binary.StringUtils //
.getBytesUtf8(login)));

connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + login);
connection.setRequestProperty("Content-Type", "plain/text");// hace
// // q
// sirva // con // el // string // de // json

connection.setRequestProperty("Content-Length",
"" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");

connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(120000); // Send
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
this.setResponseCode(connection.getResponseCode());
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();

} catch (Exception e) {

e.printStackTrace();
return null;

} finally {

if (connection != null) {
connection.disconnect();
}
}
}

这就是我所说的:
private class EntrenadorExecutioner extends AsyncTask<String, Void, String> {
private EntrenadorActivity activity = null;

public EntrenadorExecutioner(EntrenadorActivity activity) {
attach(activity);
}

private void attach(EntrenadorActivity activity) {
this.activity = activity;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
activity.startLoader();
}

@Override
protected String doInBackground(String... params) {
String json = "";

JSONObject jsonObject = new JSONObject();

try {
jsonObject.accumulate("cedula", params[0]);
jsonObject.accumulate("nombre", params[1]);
jsonObject.accumulate("primerApellido", params[2]);
jsonObject.accumulate("segundoApellido", params[3]);

json = jsonObject.toString();
} catch (JSONException je) {
je.printStackTrace();
}

return rh.POST2(Constantes.GUARDAR_ENTRENADOR, json,
Constantes.user.getUsername(),
Constantes.user.getPassword());
}

@Override
protected void onPostExecute(String response) {
activity.markAsDone();
if (response.equals("")) {
// Refresh the list view then show success
Toast.makeText(getApplicationContext(),
getString(R.string.successEntre), Toast.LENGTH_SHORT)
.show();
} else
Toast.makeText(getApplicationContext(),
getString(R.string.err_unexp), Toast.LENGTH_LONG)
.show();
}
}

最佳答案

我找到了解决方案!
我需要做的就是使用Grails用户 token ,并将其添加到 header 中,如下所示:

connection.setRequestProperty("Authorization", "Bearer " + Constantes.user.getToken()); // where get token returns the auth token

关于android - 无法将android中的POST请求发送到Grails后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com