【发布时间】:2016-05-24 10:10:27
【问题描述】:
请帮助我,我在使用 HttpUrlConnection 类发送带有 JSON 对象作为参数主体的 POST 请求时遇到问题。我一直在寻找同样的问题,我已经尝试了解决方案,但它们都不适用于我的情况。这是我的代码(我在 AsyncTask 类中调用此方法):`
public JSONObject postData(String url, JSONObject params) throws IOException{
JSONObject ret=null;
OutputStream os=null;
InputStream is=null;
HttpURLConnection cnHost=null;
URL host=null;
String jsStr=null;
StringBuilder result=null;
String json="";
try {
//TODO Open host connection
host = new URL(url);
jsStr = "key="+params;
cnHost = (HttpURLConnection) host.openConnection();
//TODO set request Attribute
//Set Connection attributes
cnHost.setDoInput(true);
cnHost.setDoOutput(true);
cnHost.setUseCaches (false);
cnHost.setAllowUserInteraction(false);
cnHost.setReadTimeout(10000);
cnHost.setConnectTimeout(15000);
cnHost.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;");
cnHost.setRequestMethod("POST");
//TODO Start streaming request
cnHost.connect();
os =cnHost.getOutputStream();
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(os));
wr.write(jsStr);
wr.flush();
wr.close();
//TODO get Response
try {
BufferedInputStream in = new BufferedInputStream(cnHost.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line); }
//TODO cast to json Object
json = result.toString();
in.close();
reader.close();
ret = new JSONObject(json);
} catch (Exception e){
Log.d("Get_response","inner_Exception");
e.printStackTrace();
}
}catch (IOException e) {
//TODO Add IOException Handler
Log.d("IOException", "Descp:");
e.printStackTrace();
}catch (NullPointerException e){
//TODO Add NUllPointerException
Log.d("NullPointerException","Descp:");
e.printStackTrace();
} catch (Exception e){
//TODO Add Exception Handler
Log.d("Exception","Descp");
e.printStackTrace();
} finally {
//region Log postData (removed when no bug)
Log.d("bodyContent:", jsStr);
Log.d("RequestMethod", cnHost.getRequestMethod());
Log.d("DoInput", String.valueOf(cnHost.getDoInput()));
Log.d("DoOutput", String.valueOf(cnHost.getDoOutput()));
Log.d("FixLengthStreamMode", String.valueOf(jsStr.getBytes().length));
Log.d("Content-Type", cnHost.getRequestProperty("Content-Type"));
Log.d("Response_Message",json);
//endregion
cnHost.disconnect();
}
return ret;
} `
下面是我调用上述方法的代码:
try{
JSONObject jsParam=new JSONObject();
jsParam.put("method", URLEncoder.encode("check","UTF-8"));
JSONObject jsResponse=postData(myUrl,jsParam);
} catch (Exception e){
e.printStackTrace;
} finally{
if(jsObj==null){
Log.d("NullJson","Yes, jsObj return null");
} else {
Log.d("ValidResponse",jsObj.toString());
}
}
当此请求从 android-app 发布时,该请求正文包含的 Web 服务始终响应为空消息。但是当我使用 Postman-client 发送它时,Web 服务响应带有有效消息。在我的邮递员代码下方:
POST /mro/public/index.php? HTTP/1.1
Host: mro.alfamartku.com
Content-Type: application/x-www-form-urlencoded;
Cache-Control: no-cache
Postman-Token: 25463d97-b821-7016-d3e7-5e256d69a31a
key={"method":"check"}
【问题讨论】:
-
发布你的 logcat 错误
-
它没有在应用程序中引发错误。应用程序成功向 Web 服务发布请求,但它返回无效消息(在我的情况下,帖子正文为空字符串)并给出“错误方法”的响应。根据 API 文档,我的帖子正文完全相同(并且正确)。我已经检查并确保帖子不包含换行符。
标签: java android json web-services httpurlconnection