Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我试图将我的android项目连接到数据库_Android_Json - Fatal编程技术网

我试图将我的android项目连接到数据库

我试图将我的android项目连接到数据库,android,json,Android,Json,我试图将我的android项目连接到php上的数据库,我的代码中没有错误,但当我试图在手机上运行我的应用程序时,它不起作用 public class Historical_Sites extends ListActivity{ private ProgressDialog pDialog; Json_Parsing jParser = new Json_Parsing(); ArrayList<HashMap<String, String>> Si

我试图将我的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);
                }
            });
        }
    }
}

你的日志与你的应用程序无关。您几乎没有
e.printStackTrace()
在catch语句中,将它们替换为
Log.e
,这样您就可以在logcat.add xml代码中看到一个异常
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;
    }
    /*
    *
    *
    *
    *
    *
    * */

}
  W/EGL_emulation: eglSurfaceAttrib not implemented



  W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface
   0xa4cce8a0, error=EGL_SUCCESS




  W/EGL_emulation: eglSurfaceAttrib not implemented



  W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface
   0xa4cce8c0, error=EGL_SUCCESS




  I/art: Background partial concurrent mark sweep GC freed 4444(333KB)
   AllocSpace objects, 3(121KB) LOS objects, 43% free, 1352KB/2MB,
   paused 1.184ms total 200.136ms



 W/art: Suspending all threads took: 7.209ms  I/Choreographer: Skipped
   91 frames!  The application may be doing too much work on its main
   thread.