2013-07-03 98 views
0

即時製作一個網站的應用程序。它有一個JSON API。試圖從中獲取結果的URL是:http://api.bayfiles.net/v1/account/login/<user>/<password>接收JSON java.lang.string不能轉換爲jsonarray

我得到錯誤:使用logcat記錄錯誤時,無法將java.lang.string轉換爲jsonarray。

我的主要活動是:

public class MainActivity extends SherlockActivity { 

    EditText un,pw; 
    TextView error; 
    Button ok; 
    private ProgressDialog mDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     un = (EditText)findViewById(R.id.user); 
     pw = (EditText)findViewById(R.id.psw); 
     ok = (Button)findViewById(R.id.button1); 
     error = (TextView)findViewById(R.id.textView1); 

     ok.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //error.setText("Clicked"); 
       //Intent startNewActivityOpen = new Intent(LoginActivity.this, FilesActivity.class); 
       //startActivityForResult(startNewActivityOpen, 0); 

       JsonAsync asyncTask = new JsonAsync(); 
       // Using an anonymous interface to listen for objects when task 
       // completes. 
       asyncTask.setJsonListener(new JsonListener() { 
        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/login/spxc/mess2005"); 



      } 
     });  
    } 

    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_id", String.valueOf(i)); 
       map.put("session", "" + e.getString("session")); 
       mylist.add(map); 
      } 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data: " + e.toString()); 
     } 

     error.setText("session"); 

       /* 
       //Intent myIntent = new Intent(ListMoviesController.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(); 
     } 
    } 

} 

這是我的適配器:JSONfunctions.java

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; 
    } 
} 

爲什麼即時通訊我收到這個錯誤?在url中使用正確的用戶名和密碼時,您將獲得:{"error":"","session":"RANDOM NUMBER"}

而且正如您所看到的,我嘗試獲取此編號。任何幫助深表感謝!

回答

1

你是因爲線

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

您要爲關鍵error獲得價值和治療是一個數組收到此錯誤,而這不是 - 這是一個空字符串。因此,你需要把它作爲一個字符串:

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

同樣,如果你需要得到你的「會話」,你可以用

String session = object.getString("session"); 

P.S.得到它請注意,這是假設您的JSONObject object實際上包含您的問題中字符串表示的對象。

+0

我應該讓它成爲對象嗎?如果是這樣,我該怎麼做? –

+0

@StianInstebo是你的json字符串嗎?如果是這樣'JSONObject job = new JSONOBject(result)'。 – Raghunandan

+0

@StianInstebo我更新了我的答案。 –

相關問題