【问题标题】:android studio doesn't connect with mysql db when configured with usb device使用usb设备配置时android studio不连接mysql db
【发布时间】:2014-07-29 07:11:19
【问题描述】:

我正在尝试将我的 android 应用程序与 mysql db 连接。如果我通过模拟器进行连接,则连接成功,但尝试使用 USB 设备(平板电脑)时却没有。它显示了这个错误:

07-29 07:12:40.651  17236-17274/com.birdorg.anpr.sdk.simple.camera.example E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:838)
     Caused by: java.lang.NullPointerException
            at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.doInBackground(AnprSdkMain.java:111)
            at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.doInBackground(AnprSdkMain.java:75)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:838)
07-29 07:12:40.886  17236-17236/com.birdorg.anpr.sdk.simple.camera.example I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x51b46238) fps:9.96, dur:1003.96, max:101.63, min:99.31
07-29 07:12:41.412  17236-17236/com.birdorg.anpr.sdk.simple.camera.example E/WindowManager﹕ Activity com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{417c01a8 V.E..... R.....ID 0,0-219,114} that was originally added here
    android.view.WindowLeaked: Activity com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{417c01a8 V.E..... R.....ID 0,0-219,114} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:409)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:218)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.onPreExecute(AnprSdkMain.java:89)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain.onClick(AnprSdkMain.java:72)
            at android.view.View.performClick(View.java:4211)
            at android.view.View$PerformClick.run(View.java:17446)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

public class AnprSdkMain extends Activity implements OnClickListener{

    private EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php login script location:

    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    //private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://10.0.2.2/anpr/webservice/login.php";

    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";

    //JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //setup input fields
        user = (EditText)findViewById(R.id.editText1);
        pass = (EditText)findViewById(R.id.editText2);

        //setup buttons
        mSubmit = (Button)findViewById(R.id.button1);

        //register listeners
        mSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
                new AttemptLogin().execute();
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AnprSdkMain.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);

                // check your log for json response
                Log.d("Login attempt", json.toString());

                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    Intent i = new Intent(AnprSdkMain.this, Lista.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                }else if(success == 0){
                    Log.d("Login Failure!", json.toString());
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(AnprSdkMain.this, file_url, Toast.LENGTH_LONG).show();
            }
        }
    }
}

我做错了什么?因为它适用于模拟器但不适用于平板电脑。我尝试将http://10.0.2.2/anpr/webservice/login.php 更改为http://127.0.0.1/anpr/webservice/login.php,但还是一样。

【问题讨论】:

    标签: android mysql database android-studio database-connection


    【解决方案1】:

    你不能那样做。如果您在 wifi 网络上,请尝试使用路由器为您的设备提供的 IP 地址。此外,您的清单还需要互联网许可。

    【讨论】:

    • 我有互联网权限。如果我想连接在线 phpmyadmin 服务器怎么办?我不能放一个ip
    • 在线服务器是什么意思?那么它肯定应该有一个IP地址或一个域名。
    • 我的意思是把 index.php 和 login.php 放在哪里?这是我第一次这样做,请给我一些指导
    • 它有一个域名,但不知道在哪里放 index.php 和10.0.2.2/anpr/webservice/login.php 应该放什么?
    • 我不清楚你的疑问。您是否从供应商处购买了自己的服务器?或者您正在本地机器上运行?您可以将两个 php 文件放在同一个目录中。
    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2011-04-10
    • 2015-05-12
    • 2014-07-31
    相关资源
    最近更新 更多