来自 android 的 XMLhttprequest 从 ATG 服务器获取响应
Posted
技术标签:
【中文标题】来自 android 的 XMLhttprequest 从 ATG 服务器获取响应【英文标题】:XMLhttprequest from android to get response from ATG server 【发布时间】:2016-10-08 12:42:02 【问题描述】:我在使用 REST API 访问 ATG sessionconfirmationNumber 时遇到了一个问题,我正在使用 http urlconnection 来实现相同的目标,代码运行良好,我也得到了输出,但是 Json 响应中的结果被四舍五入为数字很大。下面是代码。
try
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Defined URL where to send data
// URL url = new URL("http://10.201.62.27:8080/Test-Servlet/TestServletandroid?"+data);
URL url = new URL("http://52.70.41.98:7203/rest/model/atg/rest/SessionConfirmationActor/getSessionConfirmationNumber");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
HttpURLConnection httpConnection = (HttpURLConnection) conn;
Log.e("Connection", conn.toString());
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Content-Type", "text/plain");
httpConnection.connect();
// Get the server response
reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null)
// Append server response in string
sb.append(line + "\n");
response = sb.toString();
Log.v("ResponseVALID", response);
JSONObject json = new JSONObject(response);
Log.v("Tag0", json.toString());
scn = json.getLong("sessionConfirmationNumber");
/*String srr[] = response.split(":");
for(int i =0;i<srr.length;i++)
Log.v("TAG2:",srr[i]);
SCN=srr[1].replace("", "");
SCN= SCN.trim();
Log.v("SCN ",SCN);
scn=Long.parseLong(SCN);*/
catch (Exception ex)
ex.printStackTrace();
finally
try
reader.close();
catch (Exception ex)
我在门户网站上遇到了与 angularjs 相同的问题,XMLHttp 请求帮助我实现了目标。
我可以使用来自 android studio 的 XMLHttp 请求吗?如果可以!请给我一些参考,因为我已经尝试过,但没有任何结果。
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:我相信这条线:
httpConnection.setRequestProperty("Content-Type", "text/plain");
需要:
httpConnection.setRequestProperty("Content-Type", "application/json");
【讨论】:
见this线程。以上是关于来自 android 的 XMLhttprequest 从 ATG 服务器获取响应的主要内容,如果未能解决你的问题,请参考以下文章