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();
}
我想从我的android应用程序中插入一些值到mysql数据库,它显示错误“无效的IP地址”请帮忙。提前谢谢。
Logcat错误 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
答案 0 :(得分:0)
您正尝试在主线程上执行网络操作。 见How to fix android.os.NetworkOnMainThreadException?
答案 1 :(得分: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);
将此代码添加到您的代码中 然后它会工作
答案 2 :(得分:0)
我使用Angry IP扫描程序扫描路由器中的主机,获取了路由器分配给我的机器的IP地址,然后将其替换为URL; http://xxx.xxx.x.xxx/cropkeeper/spinner/soilCategory.php
这是我的尝试之物
`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();
}`
其中xxx.xxx.x.xxx
是我的IP地址。