2013-02-06 134 views
1

我在一個包中有一個Android活動,比如說com.blah.blah1和另一個包中的一個java類com.blah.blah2,I我試圖從活動訪問Java類,但它拋出的錯誤作爲找不到類'org.json.simple.parser.JSONParser' - Android

02-06 14:35:03.360:E/dalvikvm(580):找不到 類的org.json.simple .parser.JSONParser」,從法 引用com.blah.blah2.Login.login

請幫忙解決

  • 我使用eclipse
  • 我的罐子在libs文件夾
  • 我已經添加了JSON罐子到構建路徑
  • 我用的罐子JSON-簡單1.1.jar
  • 我安卓4.0.3
  • Eclipse的Java編譯版本下工作1.6

請找活動和java類下面爲你參考

public class Chat_NewActivity extends Activity { 

    private String className = "Chat_NewActivity"; 

    /** Asyn parameters */ 
    private String response; 

    /** login() parameters */ 
    LinkedHashMap<String,String> parameters ; 
    String responseToAsync = null; 
    UrlRequestAndResponse request = null; 
    static ResponseParameters param = null; 
    static String userId = null; 
    static String secureKey = null; 
    static String status = null; 
    static String sessionId = null; 

    /** Called when the activity is first created and loads main.xml content */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     Log.i(className, "Creating mail layout for the app..."); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.i(className, "Created"); 
    } 

    /** action for items selected in Preferences menu */ 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
     case R.id.settings_menu: 
      Intent settingsActivity = new Intent(getBaseContext(), 
        Chat_PreferenceActivity.class); 
      startActivity(settingsActivity); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 

    /** action calling on Start Chat button is clicked */ 
    public void startChat(View view) { 
     Log.i(className, "Start Chat button is clicked"); 
     Log.i(className, "performing action for staring chat"); 
     Log.i(className, "performing user validation"); 
     SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     Chat_AppInfo.pref_email = sharedPref.getString("email",null); 
     if (Chat_AppInfo.pref_email != null && Chat_AppInfo.pref_email.matches("[a-zA-Z0-9._-][email protected][a-z]+.[a-z]+") 
       && Chat_AppInfo.pref_email.length() > 0) { 

      /** Intent i = new Intent(getApplicationContext(), AppActivity.class); */ 

      Chat_AppInfo.pref_firstName = sharedPref.getString("firstName",null).toString(); 
      Chat_AppInfo.pref_lastName = sharedPref.getString("lastName",null).toString(); 
      EditText getsubject = (EditText) findViewById(R.id.subject); 
      Chat_AppInfo.pref_subject = getsubject.getText().toString(); 
      Log.i(className, "first name : "+Chat_AppInfo.pref_firstName); 
      Log.i(className, "last name : "+Chat_AppInfo.pref_lastName); 
      Log.i(className, "last name : "+Chat_AppInfo.pref_email); 
      Log.i(className, "subject : "+Chat_AppInfo.pref_subject); 
      if(Chat_AppInfo.pref_firstName != null || 
        Chat_AppInfo.pref_lastName != null || 
        Chat_AppInfo.pref_subject.length() > 0) { 
       Log.i(className, "validation completed!!!"); 
       checkNetworkConnection(); 
       /**i.putExtra(AppInfo.firstName, get_FirstName_From_Pref); 
       i.putExtra(AppInfo.lastName, get_LastName_From_Pref); 
       i.putExtra(AppInfo.email, get_MailId_From_Pref); 
       i.putExtra(AppInfo.connection_Url, get_MailId_From_Pref); 
       i.putExtra(AppInfo.subject, subject); 
       startActivity(i);*/ 
      } else { 
       Toast.makeText(getApplicationContext(), "Insufficient Credentials", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 
      Toast.makeText(getApplicationContext(), "E-mail is Invalid!!!", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    protected void checkNetworkConnection() { 
     Log.i(className, "Checking for Network/wi-fi connection!!!"); 
     ConnectivityManager connMgr = (ConnectivityManager) 
     getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) { 
      Log.i(className, "Network connection available!!!"); 
      new AsyncConnectionEstablisher().execute(Chat_AppInfo.service_url+"LoginAction"); 
     } else { 
      Log.i(className, "Network connection not available!!!"); 
      Toast.makeText(getApplicationContext(), "Network Connection Not Available", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private class AsyncConnectionEstablisher extends AsyncTask<String, Void, String> { 

     protected String doInBackground(String... urls) { 
      // params comes from the execute() call: params[0] is the url. 
      Log.i(className, "Performing asynchronous connection..."); 
      String output = null; 
      for (String url : urls) { 
       output = getOutputFromUrl(url); 
      } 
      return output; 
     } 

     private String getOutputFromUrl(String url) { 
      String output = null; 
      try { 
       Login createSessionObj = new Login(); 
       createSessionObj.login(url); 
       output = createSessionObj.sessionId(Chat_AppInfo.service_url+"JoinAction"); 
       //output = login(url); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return output; 
     } 
     // onPostExecute displays the results of the AsyncTask. 
     protected void onPostExecute(String result) { 
      Log.i(className, "response is : "+result); 
     } 
    } 
} 

import java.io.IOException; 
import java.util.LinkedHashMap; 

import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 

import android.util.Log; 

public class Login { 

    private String className = "Login"; 
    LinkedHashMap<String,String> parameters ; 
    String responseToAsync = null; 
    UrlRequestAndResponse request = null; 
    String response; 
    static ResponseParameters param = null; 
    static String userId = null; 
    static String secureKey = null; 
    static String status = null; 
    static String sessionId = null; 

    public void login(String url) { 
     Log.i(className, "Calling login action with service URL : "+url); 
     parameters = new LinkedHashMap<String,String>(); 
     parameters.put("firstname", Chat_AppInfo.pref_firstName); 
     parameters.put("lastname", Chat_AppInfo.pref_lastName); 
     parameters.put("email", Chat_AppInfo.pref_email); 
     parameters.put("subject", Chat_AppInfo.pref_subject); 

     request = new UrlRequestAndResponse(); 
     request.setUrlRequestParameters(url, parameters, null); 
     request.convertStreamToJson(); 
     response = request.getUrlResponseJson(); 
     try { 
      JSONObject json = (JSONObject)new JSONParser().parse(response); 
      param.setUserId((String) json.get("userId")); 
      param.setSecureKey ((String) json.get("secureKey")); 
      param.setStatus((String) json.get("status")); 

     } catch (NullPointerException e) { 
      System.out.println("Properly set the setUrlRequestParameters(url, parameters, header) , convertStreamToJson() in the request..."); 
     } catch (ParseException e) { 
      System.out.println("Parse the JSON String properly... Or Check the Servlet Response is in JSON format..."); 
     } 

     try { 
      userId = param.getUserId(); 
      secureKey = param.getSecureKey(); 
      status = param.getStatus(); 
      Log.i(className, "{\"userId\":\""+userId+"\",\"secureKey\":\""+secureKey+"\", \"status\":\""+status+"\"} "); 
      //sessionId(Chat_AppInfo.service_url+"JoinAction"); 
     } catch (NullPointerException e) { 
      System.out.println("Check the parameters in response..."); 
     } 
    } 
} 

回答