안녕하세요 . 내 웹 서비스에 json 데이터 줄이 있습니다. 모든 Json 데이터는 URL과 링크 만이 형식으로 사용할 수 있습니다. 그래서 내가 아는 것처럼 JsonObject입니다. Short는 내가 요청했고 결과는 항상 URL로 끝납니다. 따라서 출력은 다음과 같습니다.
{"Url":"www.google.com"}
이것이 내가 한 일
JSONArray json = jParser.getJSONFromUrl(url);
try {
ListBasedList.clear();
//for each loop til JSON data
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
String json_url = c.getString(TAG_Url);
if(json_url.equals(0) && json_url.equals(""))
{
LinearLayout lin_footer = (LinearLayout) findViewById(R.id.footer_layoutMain);
lin_footer.setVisibility(View.GONE);
}
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_Url, json_url);
ListBasedList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser fejl", "fejl da man prøve og hente data fra server " + e.toString());
}
return null;
}
여기에서 오류 발생
나에게 이것을 말하는 logcat :
JSONObject는 jsonarray로 변환 할 수 없습니다.
그렇다면 오류 대신 링크를 어떻게 가질 수 있습니까?
업데이트 # 1-> Logcat 전체 오류
10-01 13:34:45.685: E/JSON Parser(24256): Error parsing data org.json.JSONException: Value {"url":"www.google.com"} of type org.json.JSONObject cannot be converted to JSONArray
업데이트 # 2-> JsonParser 클래스
public class JSONParser {
static InputStream is = null;
static String json = "";
JSONArray jsonarr=null;
// konstruktor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
JSONArray jsonarr=null;
// HTTP request
try {
// defaultHttpClient
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());
}
// json array paser til string
try {
jsonarr = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retunerer json object tilbage
return jsonarr;
}
}
응답에서 단일 Json 객체를 얻습니다. 으로는 {"Url":"www.google.com"}
된 JSONObject이다.
그래서 라인
JSONArray json = jParser.getJSONFromUrl(url);
이어야한다
JSONObject json = jParser.getJSONFromUrl(url);
그리고 데이터를 읽는 동안에는
String json_url = json.getString(TAG_Url);
for 루프를 사용하는 대신.
업데이트 된 클래스보기
public class JSONParser {
static InputStream is = null;
static String json = "";
JSONObject jsonObject = null; // Updated here
// konstruktor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
jsonObject = null; // Updated here
// HTTP request
try {
// defaultHttpClient
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());
}
// json array paser til string
try {
jsonObject = new JSONObject(json); // Updated here
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retunerer json object tilbage
return jsonObject; // Updated here
}
}
이것은 현재 json에서 작동합니다. 참조 // Updated here
내가 업데이트 한 내용을 알 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다