public JSONObject getJSONFromUrl(String url) {
InputStream is = null;
JSONObject jObj = null;
String json = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
URL에서 json 문자열을 많이 봤지만 인터넷에이 질문을 할 때가되었습니다. 이 문제의 거의 모든 알고리즘이 나를 위해 작동하지 않습니다. AsyncTask와 함께 사용해야합니까? 나는 안드로이드에서 초보자이므로 많이 알지 못합니다. 도와주세요. 또는 더 나은 알고리즘을 제공 할 수 있다면 그렇게하십시오. 이 방법처럼의 BufferedReader 객체를 사용하여 읽기예기치 않게 항상 중단되었습니다.
스택 추적을 게시 할 수 있습니까? – Raghunandan
어떤 오류가 있습니까? 오류 없음 = 도움말이 없습니다. – GrIsHu
json의 형태로 서버에서 응답을 받고 있습니까? –