0

I want to add data to MySQL for Android, I use PHP service but I get in error "invalid IP adress". Can you help me?

Android code:

b1.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
              masa=e1.getText().toString();
              yemek=e2.getText().toString();
              icecek=e3.getText().toString();
              tatli=e4.getText().toString();
              insert();
          }



       });

    }
    public void insert() {


        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("masa",masa));
        nameValuePairs.add(new BasicNameValuePair("yemek",yemek));
        nameValuePairs.add(new BasicNameValuePair("icecek",icecek));
        nameValuePairs.add(new BasicNameValuePair("tatli",tatli));

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/add.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try
        {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("pass 2", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 2", e.toString());
        }

        try
        {
            JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1)
            {
                Toast.makeText(getBaseContext(), "Inserted Successfully",
                        Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getBaseContext(), "Sorry, Try Again",
                        Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
            Log.e("Fail 3", e.toString());
        }

PHP code:

    <?php

    $baglan=mysql_connect("localhost","root","");
    $sec=mysql_select_db("bitirme",$baglan);


    $masa=$_REQUEST['masa'];
    $yemek=$_REQUEST['yemek'];
    $icecek=$_REQUEST['icecek'];
    $tatli=$_REQUEST['tatli'];

    $flag['code']=0;

    if($query=mysql_query("insert into yemekler (masa,yemek,icecek,tatli) VALUES ('$masa','$yemek','$icecek','$tatli')"))
    {
        $flag['code']=1;
        echo"hi";
    }

    print(json_encode($flag));
    mysql_close($baglan);

?>
9
  • did you changed the ipaddress as per your machine? Commented Dec 4, 2014 at 10:56
  • invalid ip adress get error at try cath block.looking source codes please Commented Dec 4, 2014 at 11:01
  • @Naufal: please don't recommend that website, the tutorials contain a number of bad security practices that shouldn't be promoted (SQL injection, clear text passwords, deprecated libraries...). Commented Dec 4, 2014 at 11:02
  • Try using the ip of the machine where the server code is running. Commented Dec 4, 2014 at 11:02
  • i try used all ip4 adrees but i get same error Commented Dec 4, 2014 at 11:14

1 Answer 1

1

you can use ip address of ur pc instead of mobile ip address.

Sign up to request clarification or add additional context in comments.

Comments

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.