-2

我正在使用 eclipse,我想将我的 android 应用程序连接到我的 sql 数据库。

我的 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 上显示或显示应用程序意外停止。请再试一次。

4

1 回答 1

1

使用应该使用本地服务器的路径

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/

于 2013-10-28T12:38:28.233 回答