简体   繁体   中英

Connect app to local mysql database

I'm using this video to try and connect an android app to a locally hosted MySQL database. The php script doesn't seem to be the issue as it works fine locally (through internet explorer) and I keep on receiving a null pointer exception causing the app to crash.

The main activity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.responseTextView = (TextView) this.findViewById(R.id.responseTextView);

    new GetAllCustomerTask().execute(new ApiConnector());

}

public void setTextToTextView(JSONArray jsonArray)
{
    String s  = "";
    for(int i=0; i<jsonArray.length();i++){

        JSONObject json = null;
        try {
            json = jsonArray.getJSONObject(i);
            s = s +
                    "Name : "+json.getString("name")+"\n"+
                    "Owner : "+json.getInt("owner")+"\n"+
                    "Species : "+json.getInt("species")+"\n"+
                    "Sex : "+json.getInt("sex")+"\n"+
                    "Birth : "+json.getInt("birth")+"\n"+
                    "Death : "+json.getInt("death")+"\n";
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    this.responseTextView.setText(s);
}

private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {

         return params[0].GetAllCustomers();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {

        setTextToTextView(jsonArray);

    }
}

ApiConnector class

String url = "http://localhost/getallcustomers.php";

    HttpEntity httpEntity = null;

    try
    {

        DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();

    } catch (ClientProtocolException e) {

        e.printStackTrace();

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

    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);

            Log.e("Entity Response  : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);

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

    return jsonArray;

}

I think the problem may be with the url, but as I said before it seems to be working fine through internet explorer.

Any help would be much appreciated.

Your problem has nothing to do with Android, MySQL or PHP. In my opinion you need to take time to learn the basics of Computer Network in order to make communication possible between your computer and your mobile device.

Have a look at this article: How to connect Android with PHP MySQL

This article may help you to better understand what your real problem is. Here are the main steps that you need to think about:

  • Both machines need to be on the same network (LAN)
  • The appropriate port(s) need to be opened on your computer in order to allow inbound connection (Configuration of your firewall)

Hope this will help you to solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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