I am trying to connect to a PHP page in my Android app. I have the permissions set. However, I keep getting Invalid ip address
. It seems I need AsyncTask. However, I don't know how to set it up, maybe a class that runs separately like this: How to fix android.os.NetworkOnMainThreadException? , but on that link I have no idea why they are returning the RSSFeed
The actual website link works. It is just my app throwing this error.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "123"));
nameValuePairs.add(new BasicNameValuePair("name", "test name"));
nameValuePairs.add(new BasicNameValuePair("family_memb_id", "456");
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("app", "Failed 1: " + e.toString());
}
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();
}
catch(Exception e)
{
Log.e("app", "Failed 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("app", "Failed 3: " + e.toString());
}
Error Log:
Failed 1: android.os.NetworkOnMainThreadException
Failed 2: java.lang.NullPointerException: lock == null
Failed 3: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
This is not a duplicate of null pointer exception; although my log shows a null pointer exception, my problem happens before that. The null pointer is a side effect of what the real issue is.