【发布时间】:2017-06-25 23:29:03
【问题描述】:
早上好!
我已经使用 C# 创建了一个 REST 服务,我正在尝试从 android-studio 向 URL 发送一个发布请求。我的 URL 目前有两种方法,GET 和 POST。 GET 方法工作正常,我可以毫无问题地从 android 消费。但是当涉及到 POST 请求时,我无法从 android 使用它。
我已经使用 REST 客户端 chrome 插件测试了 POST 方法,看起来请求工作正常,我能够访问该方法并执行代码需要执行的任何内容。证据下方:
请求数据:
请求响应:
在 C# api 下:
[RoutePrefix("api/leads")]
public class LeadsController : ApiController
{
// GET: api/Leads/5
public List<Lead> Get(string matricula, int tipo)
{
List<Lead> leads = new List<Lead>();
//Fills the list
return leads;
}
[HttpPost]
// POST: api/Leads
public bool Post(int id, string usuario, string latitude, string longitude)
{
//Do stuff
return true;
}
}
最后但并非最不重要的是,我用于 POST 请求的 java 代码:
@Override
protected String doInBackground(String... strings) {
URL url = null;
try {
String newUrl = getNewUrl();
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
url = new URL(newUrl);
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.getOutputStream().write(postDataBytes);
int responseCode = conn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
server_response = readStream(conn.getInputStream());
}
} catch (MalformedURLException e) {
listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
} catch (UnsupportedEncodingException e) {
listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
} catch (ProtocolException e) {
listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
} catch (IOException e) {
listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
}
return null;
}
getNewUrl() 方法返回的 URL 是:“https://cliente.teleatlantic.com.br/CheckinLeads/api/Leads/”。
参数为:“usuario=FERNANDOCHAVES&latitude=-46.6903502&longitude=-46.6903502&id=265857”
我不知疲倦地在整个互联网(包括这个论坛)上寻找解决我的 POST 请求问题的方法。但是我找到的解决方案都没有解决我的问题。
到目前为止我已经尝试过:
1.附加“?”在 POST 请求中发送参数之前。
2.追加“/CheckinLeads/api/Leads?”在参数之前。
3.更改代码以使用HttpClient而不是HttpURLConnection。
4.将内容数据更改为“text/json”。
5. HttpURLConnection 和 HttpClient 的许多“即用型”功能。
我是 java/android 新手,如有任何帮助,我们将不胜感激。
我正在使用 4.4 Android KitKat SDK 版本 24。
感谢关注!最好的问候。
-----编辑-----
代码不会崩溃。但是当我检查请求的 HttpURLConnection.getResponseCode 时,它会显示 404。但是我发送的 URL 和参数与在 REST 客户端中测试时使用的相同。
【问题讨论】:
-
I'm not able to use it from android究竟如何?到底有什么问题?它会崩溃吗?返回错误的结果?返回错误?... -
它没有崩溃,只是没有到达服务器。如果我检查 HttpUrlConnection.getResponseCode();它向我显示了 404。但请求 URL 和参数是正确的。我看不到我错过了什么。
-
你能从androidcliente.teleatlantic.com.br/CheckinLeads/api/…Content-Type="application/x-www-form-urlencoded"中调用一次下面的静态url吗
-
@ChintanUdeshi 抱歉,我没听懂。您是说尝试将内容类型设置为“application/x-www-form-urlencoded”吗?我已经尝试过这个属性,仍然得到 404 响应。
-
能不能改一下api代码 public class test { public int id {get;set;} public string usuario {get;set;} public string latitude {get;set;} public string longitude { get ;放; } } [HttpPost] // POST: api/Leads public bool Post(test objTest) { //Do stuff return true; }