麻烦连接Android应用程序到localhost

时间:2014-07-31 01:24:21

标签: php android localhost

所以我已将笔记本电脑连接到我的机器人wifi并使用了网关IP地址,但是我试图从数据库中拔出来。数据库工作正如我单独测试的那样 Logcat建议登录时出错。 (生病标记错误出现,所以你不必)

07-31 01:49:41.323: W/System.err(20516):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-31 01:49:41.343: E/JSON Parser(20516): Error parsing data org.json.JSONException: End of input at character 0 of 
07-31 01:49:41.353: D/request!(20516): starting
07-31 01:49:41.353: W/dalvikvm(20516): threadid=11: thread exiting with uncaught exception (group=0x4194ada0)
07-31 01:49:41.353: E/AndroidRuntime(20516): FATAL EXCEPTION: AsyncTask #1
07-31 01:49:41.353: E/AndroidRuntime(20516): Process: com.example.mysqltest, PID: 20516
07-31 01:49:41.353: E/AndroidRuntime(20516): java.lang.RuntimeException: An error occured while executing doInBackground()
07-31 01:49:41.353: E/AndroidRuntime(20516):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.lang.Thread.run(Thread.java:841)
07-31 01:49:41.353: E/AndroidRuntime(20516): Caused by: java.lang.NullPointerException //this will be marked
07-31 01:49:41.353: E/AndroidRuntime(20516):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:125)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-31 01:49:41.353: E/AndroidRuntime(20516):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-31 01:49:41.353: E/AndroidRuntime(20516):    ... 4 more
07-31 01:49:42.054: W/IInputConnectionWrapper(20516): showStatusIcon on inactive InputConnection
07-31 01:49:42.474: E/WindowManager(20516): android.view.WindowLeaked: Activity com.example.mysqltest.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42da7448 V.E..... R......D 0,0-1026,288} that was originally added here
07-31 01:49:42.474: E/WindowManager(20516):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
07-31 01:49:42.474: E/WindowManager(20516):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
07-31 01:49:42.474: E/WindowManager(20516):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-31 01:49:42.474: E/WindowManager(20516):     at android.app.Dialog.show(Dialog.java:288)
07-31 01:49:42.474: E/WindowManager(20516):     at com.example.mysqltest.Login$AttemptLogin.onPreExecute(Login.java:103)  
07-31 01:49:42.474: E/WindowManager(20516):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-31 01:49:42.474: E/WindowManager(20516):     at android.os.AsyncTask.execute(AsyncTask.java:535)

package com.example.mysqltest;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login 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://192.168.43.1/drinkdealz/login.php";

    // testing from a real server:
    // private static final String LOGIN_URL =
    // "http://www.mybringback.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.login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);

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

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            break;
        case R.id.register:
            Intent i = new Intent(this, Register.class);
            startActivity(i);
            break;

        default:
            break;
        }
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.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());  ////THIS IS WHERE ERROR COMES UP////

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    // save user data
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(Login.this);
                    Editor edit = sp.edit();
                    edit.putString("username", username);
                    edit.commit();

                    Intent i = new Intent(Login.this, ReadComments.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

最后,抱歉今天发布过多,截止日期临近! 编辑:我再次运行它并得到错误:07-31 02:29:58.732:E / AndroidRuntime(26604):引起:java.lang.IllegalStateException:目标主机不能为空,或在参数中设置。 scheme = null,host = null,path = 192.168.43.1 / drinkdealz / register.php

0 个答案:

没有答案