0
try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/insert.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();
    }

I want to insert some value from my android application to mysql database and it showed an error "Invalid Ip Address" Please help. Thanks in advance.

Logcat error 09-21 12:57:12.084 912-912/com.srg.ibc.appointmentapplication E/Fail 1﹕ android.os.NetworkOnMainThreadException 09-21 12:57:12.114 912-912/com.srg.ibc.appointmentapplication E/Fail 2﹕ java.lang.NullPointerException: lock == null 09-21 12:57:12.114 912-912/com.srg.ibc.appointmentapplication E/Fail 3﹕ java.lang.NullPointerException

3 Answers3

0

You are trying to perform a networking operation on the main thread. See How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
ToasteR
  • 886
  • 7
  • 20
  • I think its not the appropriate answer in my problem. – Jet Ron Glomar Sep 22 '14 at 04:49
  • It is. `Logcat error 09-21 12:57:12.084 912-912/com.srg.ibc.appointmentapplication E/Fail 1﹕ android.os.NetworkOnMainThreadException`. The message Invalid IP Address comes from your `Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show();` The problem is that you catch all exceptions you get, so you don't get a proper error message. – ToasteR Sep 22 '14 at 05:43
  • how i can get a proper message? ithink the solution you've sent is not appropriate to my codes. – Jet Ron Glomar Sep 22 '14 at 06:56
0
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.login);

add this code into you code then it will work

0

I used Angry IP scanner to scan my router for hosts, got the IP address the router assigns to my machine , and replaced it in the URL thus ; http://xxx.xxx.x.xxx/cropkeeper/spinner/soilCategory.php

here's my try catch

`try {
        // connectivity to database
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xxx.xxx.x.xxx/cropkeeper/spinner/soilCategory.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }
    catch(Exception e) {
        Log.e("Fail 3", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show();
        finish();
    }`

where xxx.xxx.x.xxx is my IP Address.

Thorne
  • 1
  • 2