2012-03-18 100 views
0

Iam試圖使我的數據庫上的MYSQL(iam使用WAMP服務器)和Android應用程序之間的連接,但是當我運行該應用程序時引發異常,我修復了日誌貓和它的指向 jArray =新的JSONArray(結果);將android應用程序連接到服務器上的數據庫?

03-18 12:48:59.580: E/AndroidRuntime(458): at org.json.JSONArray.<init>(JSONArray.java:87) 

爲什麼會發生這種異常?以及我如何解決它?

在先進的感謝..

這是我的代碼: City.php:

  <?php 
      mysql_connect("localhost","username","password"); 
      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(); 
       ?> 

的Java類:

public class ConnectionActivity extends ListActivity { 

int ct_id; 
String[] ct_name = null; 
JSONArray jArray; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 

    // http post 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://localhost/city.php"); 
       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); 
       sb = new StringBuilder(); 
       sb.append(reader.readLine() + "\n"); 
       String line = "0"; 
       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()); 
      } 

      // paring data 

      try { 
       jArray = new JSONArray(result); 
       JSONObject json_data = null; 
       ct_name = new String[jArray.length()]; 
       for (int i = 0; i < jArray.length(); i++) { 
        json_data = jArray.getJSONObject(i); 
        ct_id = json_data.getInt("City_ID"); 
        ct_name[i] = json_data.getString("Name"); 
       } 
      } catch (JSONException e1) { 
       Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG) 
         .show(); 
      } catch (ParseException e1) { 
       e1.printStackTrace(); 
      } 

      ListView lv; 
      lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ct_name); 
      setListAdapter(adapter); 



} 
public void onListItemClick(ListView parent, View v, int position, long id) 
{ 
     Toast.makeText(this,"You have selected " + ct_name[position],Toast.LENGTH_SHORT).show(); 
     } 
}      

日誌:

   03-18 13:28:46.923: W/ActivityThread(623): Application Com.Connection is waiting for the debugger on port 8100... 
      03-18 13:28:46.973: I/System.out(623): Sending WAIT chunk 
      03-18 13:28:46.983: I/dalvikvm(623): Debugger is active 
      03-18 13:28:47.174: I/System.out(623): Debugger has connected 
      03-18 13:28:47.183: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:47.383: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:47.583: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:47.783: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:47.993: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:48.193: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:48.403: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:48.603: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:48.803: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:49.003: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:49.203: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:49.413: I/System.out(623): waiting for debugger to settle... 
      03-18 13:28:49.613: I/System.out(623): debugger has settled (1497) 
      03-18 13:30:17.593: E/log_tag(623): Error in http  connectionandroid.os.NetworkOnMainThreadException 
       03-18 13:30:34.315: E/log_tag(623): Error converting result java.lang.NullPointerException 
+0

你能告訴我們收到的JSON字符串嗎?你確定你之前沒有得到異常,結果仍然爲空? – MByD 2012-03-18 11:12:07

+0

當我運行應用程序發生異常,並將模擬器關閉它「強制關閉」 – user 2012-03-18 11:15:11

+0

行,這並沒有多大幫助。請在解析之前打印「結果」,以便我們知道它的價值。此外,異常的完整日誌也會有所幫助。 – MByD 2012-03-18 11:16:28

回答

1

,當你試圖執行主線程上的HTTP連接你得到一個錯誤:據我可以看到

03-18 13:30:17.593: E/log_tag(623): Error in http  connectionandroid.os.NetworkOnMainThreadException 

中,HTTP代碼是正確的,但你在UI上運行它線。這是不允許的。您需要在後臺線程上執行如此長的操作。 Android爲您提供了一種非常易於使用的工具,可以在後臺執行長時間操作並將結果發佈到主要(UI)線程中。其中最有用的是AsyncTask。我建議你閱讀Painless Threading,因爲它會幫助你很多IMO。

+0

我沒有很多關於通過web服務連接android和MYSQL的信息通過網絡閱讀教程,這是我做這個的第一次嘗試,我按照教程 – user 2012-03-18 12:05:50

+0

所做的那樣做,我將詳細說明我的答案。 – MByD 2012-03-18 12:06:10

+0

感謝您的幫助,我會閱讀他們,我希望連接成功 – user 2012-03-18 12:20:34

相關問題