Android应用程序连接到mysql数据库

时间:2013-11-11 09:19:57

标签: android

我尝试连接我的Android应用程序与mysql数据库,但http没有响应或它可能无法正常工作。 请建议我做什么?

不允许使用HTTP方法:http://developer.android.com/tools/extras/support-library.html/content.xml HttpClient连接错误响应代码405。

公共类MainActivity扩展了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.resultview);
    }

    public void getData() {
       String result = "";
        //the year data to send


        //http post
        try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/echo/db.php");

                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();
        }
    }
}

0 个答案:

没有答案