2013-07-04 169 views
0

即時通訊嘗試從網站BayFiles.net使用其API獲取一些信息。 呼叫網址是:http://api.bayfiles.net/v1/account/files?session=SESSION-ID可以從JSON API獲取信息

我得到的錯誤是:

的JSON輸出時正確的會話ID是這樣的:

{ 
    "error": "", 
    "S8tf": { 
     "infoToken": "wCfhXe", 
     "deleteToken": "gzHTfGcF", 
     "size": 122484, 
     "sha1": "8c4e2bbc0794d2bd4f901a36627e555c068a94e6", 
     "filename": "Screen_Shot_2013-07-02_at_3.52.23_PM.png" 
    }, 
    "S29N": { 
     "infoToken": "joRm6p", 
     "deleteToken": "IL5STLhq", 
     "size": 129332, 
     "sha1": "b4a03897121d0320b82059c36f7a10a8ef4c113d", 
     "filename": "Stockholmsyndromet.docx" 
    } 
} 

但是我不能讓趕respons並以listview顯示它。 這是我的活動:

public class FilesActivity extends SherlockListActivity implements 
    OnClickListener { 

     private ProgressDialog mDialog; 
     ActionBar ABS; 
     TextView session; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.dblist); 

      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setTitle("Files"); 

      JsonAsync asyncTask = new JsonAsync(); 
      // Using an anonymous interface to listen for objects when task 
      // completes. 
      asyncTask.setJsonListener(new JsonListener() { 
       @Override 
       public void onObjectReturn(JSONObject object) { 
        handleJsonObject(object); 
       } 
      }); 
      // Show progress loader while accessing network, and start async task. 
      mDialog = ProgressDialog.show(this, getSupportActionBar().getTitle(), 
        getString(R.string.loading), true); 
      asyncTask.execute("http://api.bayfiles.net/v1/account/files?session=" + PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("sessionID", "defaultStringIfNothingFound")); 


      //session = (TextView)findViewById(R.id.textView1); 
      //session.setText(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("sessionID", "defaultStringIfNothingFound")); 

     } 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 

     private void handleJsonObject(JSONObject object) { 
      ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

      try { 

       JSONArray shows = object.getJSONArray("error"); 

       for (int i = 0; i < shows.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        JSONObject e = shows.getJSONObject(i); 


        //map.put("video_location", "" + e.getString("video_location")); 
        mylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 

      ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.dbitems, 
        new String[] { "video_title", "video_location" }, new int[] { R.id.item_title, 
          R.id.item_subtitle }); 

      setListAdapter(adapter); 

      final ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        @SuppressWarnings("unchecked") 
        HashMap<String, String> o = (HashMap<String, String>) lv 
          .getItemAtPosition(position); 

        //Intent myIntent = new Intent(ListShowsController.this, 
         // TestVideoController.class); 
        //myIntent.putExtra("video_title", o.get("video_title")); 
        //myIntent.putExtra("video_channel", o.get("video_channel")); 
        //myIntent.putExtra("video_location", o.get("video_location")); 
        //startActivity(myIntent); 
       } 
      }); 

      if (mDialog != null && mDialog.isShowing()) { 
       mDialog.dismiss(); 
      } 
     } 
    } 

and my JSONfunctions: 
public class JSONfunctions { 

    public static JSONObject getJSONfromURL(String url){ 
     InputStream is = null; 
     String result = ""; 
     JSONObject jArray = null; 

     //http post 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 

      try { 
       // Add your data 
       /*List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("key", "stianxxs")); 
       nameValuePairs.add(new BasicNameValuePair("secret", "mhfgpammv9f94ddayh8GSweji")); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); */ 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 
       //HttpResponse response = httpclient.execute(httppost); 
       HttpEntity httpEntity = response.getEntity(); 
       is = httpEntity.getContent(); 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
      } 

     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     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(); 
       result=sb.toString(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     try{ 

      jArray = new JSONObject(result);    
     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

     return jArray; 
    } 
} 

任何幫助非常感謝!

回答

1

由於'錯誤'不是JSONArray它給你解析錯誤。

JSONArray shows = object.getJSONArray("error"); 

更改你行來

String shows = object.getString("error"); 

您可以參考這些鏈接,JSON解析。 https://stackoverflow.com/a/16938507/1441666

+0

解決了這個錯誤,無論誰仍然沒有顯示任何東西,我希望將行分開:'JSONObject e = shows.getJSONObject(i);'然後我不能使用:'map.put(「filename 「,」「+ e.getString(」filename「));' –

+0

'shows'不是JSON對象。這只是一個字符串。我認爲你必須先獲得名爲'S8tf'的JSON對象。我建議你先從基本的JSON教程中學習;有一些基本概念需要理解。 –