0
그래서 java와 android에서 똑같은 코드를 사용합니다. 그러나 어떤 이유로 안드로이드 메소드 get()
은 항상 작업을 수행하지 못하고 false를 반환하지만 표준 java에서는 완벽하게 작동합니다. 그리고 나는 매니페스트에서 인터넷 허가를 가지고있다.BufferedReader는 표준 자바에서 작동하지만 안드로이드에서는 작동하지 않습니다
public boolean get(){
try {
jsonObject = new JSONObject(readUrl(url)); // Ignore JSON thing.
return true;
} catch (Exception ex) {
return false;
}
}
private String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
return buffer.toString();
} finally {
if (reader != null) {
reader.close();
}
}
}
AndroidManifest.xml에 인터넷 권한을 추가 했습니까? –
예, 언급하지 않았습니다. –
스택 추적을 인쇄하고 질문에 추가하십시오. NetworkOperationNotallowedInUiThread 일 수도 있습니다. –