-1
從REST服務器獲取JSON
數據時遇到問題。 Web瀏覽器返回JSON
數據here正確,但下面的代碼將導致FileNotFound
例外:從Android的REST服務器獲取JSON時發生FileNotFound異常
private void getJSON(String url) {
class GetJSONServices extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = new ProgressDialog(ServicesActivity.this);
loading.setTitle("");
loading.setMessage("Getting your services...");
loading.setCancelable(false);
loading.show();
}
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}
return sb.toString().trim();
}catch(Exception e){
Log.e("Do In Background", getStackTrace(e));
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
Toast.makeText(ServicesActivity.this, s, Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Log.e("On Post Execute", getStackTrace(e));
}
}
}
GetJSONServices gj = new GetJSONServices();
gj.execute(url);
}
此外,AndroidManifest.xml中有INTERNET權限,其餘服務器允許CORS。是什麼導致了這個錯誤,我該如何解決?
編輯:我使用HttpURLConnection,而不是刪除的HttpClient。
什麼是'sb.toString()'的輸出? –
[在Web服務中讀取Json在Android中休息。 FileNotFoundException](http://stackoverflow.com/questions/11978412/reading-json-in-webservice-rest-in-android-filenotfoundexception) – Sree
發佈logcat – Pankaj