0

I'm trying to connect android with php and also mysql using json. This is apart of my coding which is to add data.

Based on the coding

private static String url_create_fixture = "http:// android_connect/create_product.php";

What should be the ip address if I'm testing this application through android phone instead of the emulator?

Another question, I tested this application on the phone and it says that process.com.example.mobile_fixtures has stopped working unexpectedly. Please try again.

Is this because of the wrong ip or there is something wrong with my process?

package com.example.mobile_fixtures;

public class NewFixturesActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputDate;
EditText inputTime;
EditText inputStadium;
EditText inputHome;
EditText inputAway;


// url to create new product
private static String url_create_fixtures = "http://10.1.1.6/android_connect/create_product.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_fixtures);

    // Edit Text
    inputDate = (EditText) findViewById(R.id.inputDate);
    inputTime = (EditText) findViewById(R.id.inputTime);
    inputStadium = (EditText) findViewById(R.id.inputStadium);
    inputHome = (EditText) findViewById(R.id.inputHome);
    inputAway = (EditText) findViewById(R.id.inputAway);

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NewFixturesActivity.this);
        pDialog.setMessage("Creating Fixtures..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String date = inputDate.getText().toString();
        String time = inputTime.getText().toString();
        String stadium = inputStadium.getText().toString();
        String home = inputHome.getText().toString();
        String away = inputAway.getText().toString();


        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("date", date));
        params.add(new BasicNameValuePair("time", time));
        params.add(new BasicNameValuePair("stadium", stadium));
        params.add(new BasicNameValuePair("home", home));
        params.add(new BasicNameValuePair("away", away));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_fixtures,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(), AllFixturesActivity.class);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}
}
1
  • Please show the error log. As for the ip address, you should use the ip of the machine your website is hosted on. Commented Nov 19, 2013 at 14:07

2 Answers 2

0

Do not launch a new UI activity in the doInBackground move it into the postExecute instead.

2
  • Sorry but may I ask. Why should I use postExecute instead? Commented Nov 19, 2013 at 14:59
  • postExecute is executed on the UI thread whereas doInBackground is on a different thread to the UI which will cause problems. @amln_ndh Commented May 12, 2014 at 12:02
0

If you are running a php server on your pc, the pc should be reachable from your phone. 10.1.1.6 is an internal network ip-address, so probably you cannot reach it from your phone.

3
  • Sorry but I don't really understand because I'm new to this. So how do I made the phone reachable from my phone? Commented Nov 19, 2013 at 14:49
  • Lots of options: - make sure your phone is connected to the same network as your pc (check if your phones ip address also starts with 10) - OR create a wifi access point on your pc, so you can connect your phone through that access point - OR deploy the php code to a webserver and connect to that server Commented Nov 19, 2013 at 15:35
  • Thank you @Fortega :) I checked my phone ip and it is the same as my pc which is 10.1.1.6. So does this mean that my phone is reachable from pc? Commented Nov 19, 2013 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.