简体   繁体   English

运行我的android项目时出错

[英]error in running my android project

I was trying to connect my android project to database on the php there is no errors in my code but when i am trying to run my app on phone it is not working 我试图将我的android项目连接到php上的数据库,但是我的代码中没有错误,但是当我尝试在手机上运行我的应用程序时却无法正常工作

public class Historical_Sites extends ListActivity{
    private ProgressDialog pDialog;
    Json_Parsing jParser = new Json_Parsing();
    ArrayList<HashMap<String, String>> SitesList;

    private static String url_all_Sites = "http://tourin.esy.es/php/historical.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_SITE_NO="Site_NO";
    private static final String TAG_SITE_NAME = "Site_Name";

    JSONArray Sites = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.historical_sites);
        SitesList = new ArrayList<HashMap<String, String>>();
        new LoadAllProducts().execute();

        ListView lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String sitname = ((TextView) view.findViewById(R.id.sitenam)).getText()
                        .toString();
                Intent in = new Intent(getApplicationContext(),
                        Check.class);
                in.putExtra(TAG_SITE_NAME, sitname);
                startActivityForResult(in, 100);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100) {
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Historical_Sites.this);
            pDialog.setMessage("Loading Historical Sites. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONObject json = jParser.makeHttpRequest(url_all_Sites, "GET", params);
            Log.d("All Sites: ", json.toString());
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {

                    Sites = json.getJSONArray(TAG_POSTS);
                    for (int i = 0; i < Sites.length(); i++) {
                        JSONObject c = Sites.getJSONObject(i);

                         String sitno= c.getString(TAG_SITE_NO);
                        String Sitenam = c.getString(TAG_SITE_NAME);

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_SITE_NO, sitno);
                        map.put(TAG_SITE_NAME, Sitenam);
                        SitesList.add(map);
                    }
                } else {

                    Intent i = new Intent(getApplicationContext(),
                            Chat.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            Historical_Sites.this, SitesList,
                            R.layout.historical_sites_rows, new String[] {TAG_SITE_NAME},
                            new int[] { R.id.sitenam });

                    setListAdapter(adapter);
                }
            });
        }
    }
}

this the Json_Parsing 这是Json_Parsing

public class Json_Parsing {   

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    // constructor
    public Json_Parsing() { }

    public JSONObject makeHttpRequest(String url, String method,
                                      List<NameValuePair> params) {
        try {
            if(method == "POST"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent();
            }else if(method == "GET"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace(); } catch (IOException e) {
            e.printStackTrace();
        }
        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");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;
    }

    public JSONObject getJSONFromUrl(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        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");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;
    }
    /*
    *
    *
    *
    *
    *
    * */

}

i and this is my error in the 'LogCat' 我,这是我在“ LogCat”中的错误

E/ViewRootImpl: sendUserActionEvent() mView == null E / ViewRootImpl:sendUserActionEvent()mView == null

protected void onPostExecute(String file_url) {

            pDialog.dismiss();


                    ListAdapter adapter = new SimpleAdapter(
                            Historical_Sites.this, SitesList,
                            R.layout.historical_sites_rows, new String[] {TAG_SITE_NAME},
                            new int[] { R.id.sitenam });

                    setListAdapter(adapter);

        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM