【发布时间】:2016-01-07 06:18:43
【问题描述】:
我的 web api 开发人员向我提供了这个 url notworking webserice link 我正在尝试像这样使用 AsyncTask 来使用它:
@Override
protected String doInBackground(String... params) {
StringBuffer response = new StringBuffer();
this.url="http://hellosewa.com/slashapp/public/api/questions/1";
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
Log.d("async update", "\nSending 'GET' request to URL : " + con);
Log.d("async update", "\nSending 'GET' request to URL : " + url);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
Log.d("async date", "\nSending 'GET' request to URL : " + url);
Log.d("async date", "Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}catch (MalformedURLException e) {
Log.d("async date", "MalformedUURL Exception" + e);
e.printStackTrace();
} catch (IOException e) {
Log.d("async date", "IO Exception" + e);
e.printStackTrace();
}
Log.d("async date","response=="+response.toString());
return response.toString();
}
我不知道为什么它在 webbrowser 中看起来不错的 JSON 时返回 500 响应代码。我的代码可以正常使用此链接 working_link 的 webservice
【问题讨论】:
-
设置
con.setRequestMethod("GET");后试一下,去掉`con.setDoOutput(true);`行
标签: android json web-services