2013-04-03 5 views
1

Ниже мой код:в org.json.JSON.typeMismatch (JSON.java:107)

public class JSON extends Activity { 
    TextView json; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String twitterTimeline = getTwitterTimeline(); 
     try { 
      String tweets = ""; 
      JSONObject jObj = new JSONObject(twitterTimeline); 
      JSONArray jsonArray = jObj.getJSONArray("results"); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       int j = i+1; 
       tweets +="*** " + j + " ***\n"; 
       tweets += "Date:" + jsonObject.getString("trends") + "\n"; 
       tweets += "Post:" + jsonObject.getString("name") + "\n\n"; 
      } 
      json= (TextView)findViewById(R.id.json); 
      json.setText(tweets); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
    public String getTwitterTimeline() { 
     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet("https://api.twitter.com/1/trends/23424848.json"); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        builder.append(line); 
       } 
      } else { 
       //Couldn't obtain the data 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return builder.toString(); 
    } 
} 

I'am сообщение получать сообщение об ошибке:

Неправильный заголовок печенья : "set-cookie: guest_id = v1% 3A136496691433468960; Домен = .twitter.com; Path = /; Expires = Fri, 03-Apr-2015 05:28:34 UTC". Невозможно разобрать атрибут expires: Пт, 03-апр-2015 05:28:34 UTC org.json.JSONException: Значение [{"as_of": "2013-04-03T05: 28: 34Z", "тенденции" : [{ "события": нулевая, "запрос": "% 22Finding + Дори% 22", "URL": "http://twitter.com/search?q=%22Finding+Dory%22", "promoted_content" : null, "name": "Поиск Dory"}, {"events": null, "query": "% 23MentionADislike", "url": "http://twitter.com/search?q=%23MentionADislike" "promoted_content": нулевой, "имя": "#MentionADislike"}, { "события": NULL, "запрос": "% 23PSG", "URL": "http://twitter.com/search?q= % 23PSG " "promoted_content": нулевой, "имя": "#PSG"}, { "события": нулевая, "запрос": "% 23IPLOpeningCeremony", "URL":" http://twitter.com/search ? д =% 23IPLOpeningCeremony " "promoted_content": нулевой, "имя": "#IPLOpeningCeremony"}, { "события": нулевая, "запрос": "% 23UCL", "URL":" HTTP: // твиттер. ком/поиск д =% 23UCL " "promoted_content?": NULL, "имя": "# UCL"}, { "события": нулевая, "запрос": "% 23GamesWePlay", "URL":" HTTP : //twitter.com/search д =% 23GamesWePlay», "promoted_content": нулевой, "имя": "#GamesWePlay"}, { "события": нулевая, "запрос": "Юве", "URL": "http://twitter.com/search?q=Juve","promoted_content":null,"name":"Juve"},{"events":null,"query":"Valdes","url": "http://twitter.com/search?q=Valdes","promoted_content":null,"name":"Valdes"},{"events":null,"query":"Barca","url": "http://twitter.com/search?q=Barca","promoted_content":null,"name":"Barca"},{"events":null,"query":"Bayern","url": «http://twitter.com/search?q=Bayern","promoted_content":null,"name":"Bayern"}],"locations":[{"woeid":23424848,"name":"India "}]," created_at ":" 2013-04-03T05: 19: 03Z "}] типа org.json.JSONArray не может быть преобразован в JSONObject по адресу org.json.JSON.typeMismatch (JSON.java:107)

может кому угодно g у меня, где я делаю неправильно?

Этот формат JSON как Разобрать и дисплей

[ 
{ 
"trends":[ 
{ 
"name":"#PappuCII", 
"url":"http:\/\/twitter.com\/search?q=%23PappuCII", 
"promoted_content":null, 
"query":"%23PappuCII", 
"events":null 
}, 
{ 
"name":"#ReplaceMovieNamesWithKamina", 
"url":"http:\/\/twitter.com\/search?q=%23ReplaceMovieNamesWithKamina", 
"promoted_content":null, 
"query":"%23ReplaceMovieNamesWithKamina", 
"events":null 
}, 

плз Помоги мне я получаю путать сейчас ..

ответ

4

как в результатах LogCat:

JSONArray не могут быть преобразованы к JSONObject

, потому что вы получаете JSONArray как корневой элемент в текущей строке json вместо JSONObject. так что вам нужно будет сначала преобразовать текущую строку в JSONArray затем извлечь все JSONObject из it.chnage код, как:

JSONArray jArray = new JSONArray (twitterTimeline); //<convert string to JSONArray 
for (int i = 0; i < jArray .length(); i++) { 
    // get all JSONObject from jArray here.. 
} 

текущий формат JSON является:

[ //<<<< this is JSONArray 
    { //<<<< this is JSONObject inside JSONArray 


    }, 
..... 
] 
+0

ρяσѕρєя K сэр может сказать мне в деталях , на самом деле я новичок в android. – Nandu

+0

проблема ... Может кто-нибудь мне помочь? – Nandu

+0

почему эта ошибка «org.json.JSONException: нет значения для имени» ??? конвертируя этот URL-адрес "https://api.twitter.com/1/trends/23424848.json " – Nandu

Смежные вопросы