我在一个包中有一个Android活动,比如com.blah.blah1,在另一个包里有一个java类,比如com.blah.blah2,我试图从该活动访问java类,但它抛出错误作为
02-06 14:35:03.360:E/dalvikvm(580):找不到类"org.json.simple.parser.JSONParser",从方法中引用com.blah.blah2.登录
请帮助解决
- 我正在使用eclipse
- 我的罐子在libs文件夹中
- 我已经将json-jar添加到构建路径中
- 我使用的是jar json-simple-1.1.jar
- 我在Android 4.0.3下工作
- eclipsejava编译器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._-]+@[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...");
}
}
}
您确定在应用程序中包含了org.json.simple.parser.JSONParser
类吗?因为它似乎不在android的默认json包上http://developer.android.com/reference/org/json/package-summary.html因此,也许您应该尝试手动包含这些类http://code.google.com/p/json-simple/source/browse/trunk/src/org/json/simple/parser/JSONParser.java?r=73