如何在连接android到MYSQL时解决类未找到异常?

时间:2012-03-18 19:30:26

标签: php android mysql json

我正在尝试在WAMP服务器上建立android和我的数据库之间的连接,当我运行应用程序时,会发生异常,为什么会发生这种情况?以及如何解决它?我在网上搜索了很多,我查看了更多次代码,当我运行php脚本时,它成功运行并返回数据,但异常仍然存在

这是我的代码

city.PHP

<?php
mysql_connect("localhost","username","pass");
mysql_select_db("Deal");
$sql=mysql_query("select * from City where Name like 'R%' ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

City.java

public class City extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    parseJSON();

}


void parseJSON()
{
    String result = "";
    String x = "";
    InputStream is=null;
//http post
try{
ArrayList nameValuePairs = new ArrayList();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/city.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
    json_data = jArray.getJSONObject(i);
  Log.i("log_tag","id: "+json_data.getString("City_ID")+", name:    "+json_data.getString("Name"));
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}

logcat的

03-18 22:12:21.553: W/dalvikvm(491): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-18 22:12:21.583: E/AndroidRuntime(491): FATAL EXCEPTION: main
03-18 22:12:21.583: E/AndroidRuntime(491): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.JSON/com.JSON.JSONexActivity}: java.lang.ClassNotFoundException: com.JSON.JSONexActivity in loader dalvik.system.PathClassLoader[/data/app/com.JSON-2.apk]
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.os.Looper.loop(Looper.java:132)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-18 22:12:21.583: E/AndroidRuntime(491):  at java.lang.reflect.Method.invokeNative(Native Method)
03-18 22:12:21.583: E/AndroidRuntime(491):  at java.lang.reflect.Method.invoke(Method.java:491)
03-18 22:12:21.583: E/AndroidRuntime(491):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-18 22:12:21.583: E/AndroidRuntime(491):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-18 22:12:21.583: E/AndroidRuntime(491):  at dalvik.system.NativeStart.main(Native Method)
03-18 22:12:21.583: E/AndroidRuntime(491): Caused by: java.lang.ClassNotFoundException: com.JSON.JSONexActivity in loader dalvik.system.PathClassLoader[/data/app/com.JSON-2.apk]
03-18 22:12:21.583: E/AndroidRuntime(491):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
03-18 22:12:21.583: E/AndroidRuntime(491):  at java.lang.ClassLoader.loadClass(ClassLoader.java:540)
03-18 22:12:21.583: E/AndroidRuntime(491):  at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
03-18 22:12:21.583: E/AndroidRuntime(491):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
03-18 22:12:21.583: E/AndroidRuntime(491):  ... 11 more

1 个答案:

答案 0 :(得分:1)

显然,您的项目中没有名为com.JSON.JSONexActivity的类,并且正在尝试启动该活动。