简体   繁体   English

我无法在android studio模拟器中甚至在手机中作为运行设备来运行该应用程序。 不幸的是,数据库程序已停止

[英]I wouldn't able to run the app in android studio emulator and even in my phone as running devices. the database Program is unfortunately stopped

public class MainActivity extends ActionBarActivity
 {
        String myJSON;
        pri`enter code here`vate static final String TAG_RESULTS="result";
        private static final String TAG_ID = "id";
        private static final String TAG_NAME = "name";
        private static final String TAG_ADD ="address";
        JSONArray peoples = null;
        ArrayList<HashMap<String, String>> personList;
        ListView list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            list = (ListView) findViewById(R.id.listView);
            personList = new ArrayList<HashMap<String,String>>();
            getData(); }
        protected void showList(){
            try {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String address = c.getString(TAG_ADD);

                HashMap<String,String> persons = new HashMap<String,String>();

                persons.put(TAG_ID,id);
                persons.put(TAG_NAME,name);
                persons.put(TAG_ADD,address);

                personList.add(persons);
            }

            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, personList, R.layout.list_item,
                    new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                    new int[]{R.id.id, R.id.name, R.id.address}
            );

            list.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = null;
                try {
                    httppost = new HttpPost("http://127.0.0.1/database.php");
                } catch (Exception e)
                {
                    e.getMessage();
                }

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e)
                {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','person_db');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from Persons";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_row($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'address'=>$row[2]
));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

?>

在两个设备上连接相同的WIFI网络

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不幸, <your app> 已停止,而我正在尝试在我的android模拟器中运行它。 如何解决呢? - unfortunately, <your app> has stopped while i am trying it run it in my android emulator. how to solve this? 不幸的是我的应用程序已在android模拟器中停止了? - unfortunately my app has stopped in android emulator? 不幸的是,我的应用已停止[Android Emulator] - Unfortunately My app has stopped [Android Emulator] 当我在android studio中运行程序时出现错误“不幸的是,我的应用程序已停止” - There is an error “unfortunately, my application has stopped” when i run my program in android studio 我想在android中实现SeekBar,但是当我运行以下程序时,我的模拟器显示错误“不幸的是SeekBar已停止” - I want to implement SeekBar in android ,But when i run the below program my emulator shows error that “Unfortunately SeekBar has been stopped” Android Studio:不幸的是“我的应用程序”已停止 - Android Studio: Unfortunately "my App" has stopped Android Studio:应用程序未在模拟器中启动“很遗憾,.. app ..已停止” - Android Studio: App doesn't start in emulator “Unfortunately ..app.. has stopped” 不幸的是,当该应用在genymotion模拟器上运行时,android已停止工作 - Unfortunately android has stopped working when the app is run on genymotion emulator 不幸的是,当使用模拟器运行时,已停止了应用程序android - unfortunately has stopped app android when run with emulator APK将无法在Android模拟器上运行。 (很遗憾,该应用已停止) - The APK won't run on the android emulator. (Unfortunately the app has stopped)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM