2013-10-28 95 views
-2

我使用eclipse,我想連接我的android應用程序到我的sql數據庫。android應用程序連接到mysql數據庫

我activity.java代碼

public class MainActivity extends Activity { 

    TextView resultView; 
    private InputStream is; 

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

     resultView=(TextView) findViewById(R.id.action_settings); 
    } 

    public void getData() { 
     String result = ""; 
     //the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("name","suman")); 

     //http post 
     try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://127.0.0.1/hello/try.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

       Log.e("log_tag", "connection success "); 
       Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); 
     } catch(Exception e) { 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
       Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show(); 
     } 
     //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"); 
         Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show(); 
       } 
       is.close(); 

       result=sb.toString(); 
     } catch(Exception e) { 
       Log.e("log_tag", "Error converting result "+e.toString()); 
      Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show(); 
     } 

     //parse json data 
     try { 
      JSONArray jArray = new JSONArray(result); 

      for(int i=0;i<jArray.length();i++){ 
        JSONObject json_data = jArray.getJSONObject(i); 
        Log.i("log_tag","id: "+json_data.getInt("id")+ 
          ", Name: "+json_data.getString("name")+ 
          ", Address: "+json_data.getInt("address")+ 
          ", Contact: "+json_data.getInt("contact") 
        ); 
        Toast.makeText(getApplicationContext(), "JsonArray pass", Toast.LENGTH_SHORT).show(); 
      } 
     } catch(JSONException e) { 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
       Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

但是運行之後的應用數據庫沒有顯示在AVD或展示的應用程序停止unexpectectedly。請再試一次。

+3

請出示導致應用崩潰異常。你可以在日誌中找到它。 –

+0

10-28 18:26:06.026:D/SntpClient(72):請求時間失敗:java.net.SocketException:協議不支持的地址系列 10-28 18:31:06.091:D/SntpClient(72)請求時間失敗:java.net.SocketException:地址族不支持的協議 –

+0

仍然這是一個問題plz幫助我: - 錯誤顯示在logcat是java.lang.NullPointerException'runnig android lint'遇到問題 - 失敗 –

回答

1

使用時應使用路徑的LocalServer

http://10.0.2.2/[your application folder on server]/[php file(.php)]; 

http://10.0.2.2 /hello/try.php 

,而不是

http://127.0.0.1/hello/try.php 

請參閱例如

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

+0

thanx nitin,我已經應用這個但相同的結果 –

+0

@sumankumar嘗試使用這個androidhive你會得到結果,我已經使用這個代碼爲我的應用程序。 –

+0

仍然這是一個問題plz幫助我: - 錯誤顯示在logcat是java.lang.NullPointerException'runnig android lint'遇到了問題 - 失敗 –

相關問題