I am trying to connect to the server but the app is not connecting, I add the privilges
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and this the url address(you can connect to it )
http://justedhak.comlu.com/insert.php
and this is the error:
06-30 08:21:15.332: I/System.out(4270): main calls detatch()
06-30 08:21:15.332: E/Fail 1(4270): android.os.NetworkOnMainThreadException
06-30 08:21:15.337: E/Fail 2(4270): java.lang.NullPointerException: lock == null
06-30 08:21:15.337: E/Fail 3(4270): java.lang.NullPointerException
06-30 08:21:16.802: I/System.out(4270): main calls detatch()
06-30 08:21:16.802: E/Fail 1(4270): android.os.NetworkOnMainThreadException
06-30 08:21:16.807: E/Fail 2(4270): java.lang.NullPointerException: lock == null
06-30 08:21:16.807: E/Fail 3(4270): java.lang.NullPointerException
this is my code:
public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/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();
}
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());
}
}
what could be missing ?