【问题标题】:Invalid IP Address Android insert to MySQL无效的IP地址Android插入MySQL
【发布时间】:2014-11-15 16:31:33
【问题描述】:
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

【问题讨论】:

    标签: android mysql insert ip


    【解决方案1】:

    您正试图在主线程上执行网络操作。 见How to fix android.os.NetworkOnMainThreadException?

    【讨论】:

    • 我认为这不是我的问题的合适答案。
    • 是的。 Logcat error 09-21 12:57:12.084 912-912/com.srg.ibc.appointmentapplication E/Fail 1﹕ android.os.NetworkOnMainThreadException。消息 Invalid IP Address 来自您的 Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show(); 问题是您捕获了所有异常,因此您没有收到正确的错误消息。
    • 我怎样才能得到正确的消息?我认为您发送的解决方案不适合我的代码。
    【解决方案2】:
    @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);
    

    将此代码添加到您的代码中 然后它会工作

    【讨论】:

      【解决方案3】:

      我使用 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 地址。

      【讨论】:

        猜你喜欢
        • 2014-08-15
        • 2014-01-12
        • 1970-01-01
        • 2016-12-01
        • 1970-01-01
        • 2013-11-16
        • 2012-06-05
        • 2021-12-13
        • 1970-01-01
        相关资源
        最近更新 更多