Android应用在访问localhost时崩溃

时间:2015-07-25 10:25:05

标签: java php android post login

抱歉我的英语不好..这是我的第一篇文章:D 但是我在Android / Java编程中遇到了很大的问题,而且我在Android编程方面真的很新......

我尝试将数据发送到外部PHP服务器(在同一网络中)并使用SharedPreferences保存。但是,如果按下"登录" -Button,应用程序将始终崩溃。

这是我的代码: Login.class

public class Login extends ActionBarActivity {
    final Context context = this;
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            final Button btn_login = (Button) findViewById(R.id.btn_login);
            final EditText username = (EditText) findViewById(R.id.login_username);
            final EditText password = (EditText) findViewById(R.id.login_password);
            final EditText password2 = (EditText) findViewById(R.id.login_password2);
            final EditText ip = (EditText) findViewById(R.id.login_ip);

            btn_login.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                        // Create a new HttpClient and Post Header
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(ip.getText().toString()+"/app/android/login.php");
                        JSONObject json = new JSONObject();

                        try {
                            // JSON data:
                            json.put("username", username.getText().toString());
                            json.put("password", password.getText().toString());

                            JSONArray postjson=new JSONArray();
                            postjson.put(json);

                            // Post the data:
                            httppost.setHeader("json",json.toString());
                            httppost.getParams().setParameter("jsonpost",postjson);

                            // Execute HTTP Post Request
                            System.out.print(json);
                            @SuppressWarnings("deprecation")
                            HttpResponse response = httpclient.execute(httppost);

                            // for JSON:
                            if(response != null){
                                InputStream is = response.getEntity().getContent();

                                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                StringBuilder sb = new StringBuilder();

                                String line = null;
                                try {
                                    while ((line = reader.readLine()) != null) {
                                        sb.append(line + "\n");
                                    }
                                } catch (IOException e) {
                                    Toast.makeText(getApplicationContext(), "Error!"+e, Toast.LENGTH_LONG).show();
                                } finally {
                                    try {
                                        is.close();
                                    } catch (IOException e) {
                                        Toast.makeText(getApplicationContext(), "Error!"+e, Toast.LENGTH_LONG).show();
                                    }
                                }
                                String text = sb.toString();

                                if(text == "true"){
                                    //save Username in Preferences
                                     savePreferences("username", username.getText().toString());
                                    //save password in Preferences 
                                     savePreferences("password", password.getText().toString());
                                    //save password in Preferences 
                                     savePreferences("ip", ip.getText().toString());


                                    Intent i = new Intent(Login.this, Start.class);

                                    startActivity(i);
                                }
                                else if(text == "405"){
                                    Toast.makeText(getApplicationContext(), "Error while saving data", Toast.LENGTH_LONG).show();
                                }
                                else if(text == "406"){
                                    Toast.makeText(getApplicationContext(), "Passwords aren't the same!", Toast.LENGTH_LONG).show();
                                }
                                else if(text == "true408"){
                                    //save Username in Preferences
                                     savePreferences("username", username.getText().toString());
                                    //save password in Preferences 
                                     savePreferences("password", password.getText().toString());
                                    //save password in Preferences 
                                     savePreferences("ip", ip.getText().toString());

                                    Toast.makeText(getApplicationContext(), "Wieder eingeloggt!", Toast.LENGTH_LONG).show();
                                    Intent i = new Intent(Login.this, Start.class);

                                    startActivity(i);
                                }
                                else if(text == "407"){
                                    Toast.makeText(getApplicationContext(), "Passwort falsch!", Toast.LENGTH_LONG).show();
                                }
                                else if(text == "404"){
                                    Toast.makeText(getApplicationContext(), "Fehler beim abgleich!", Toast.LENGTH_LONG).show();
                                }
                            }


                        }catch (IOException e) {
                            Toast.makeText(getApplicationContext(), "Fehler beim Login!"+e, Toast.LENGTH_LONG).show();
                        } catch (JSONException e1) {
                            Toast.makeText(getApplicationContext(), "Fehler beim Login!"+e1, Toast.LENGTH_LONG).show();
                        }

                }

            });
        } 

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.login, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.menu_login_help) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                    // set title
                    alertDialogBuilder.setTitle("Hilfe");

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("This is the Tutorial of the App")
                        .setCancelable(true)
                        ;

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        private void savePreferences(String key, String value) {
              SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
              Editor editor = sharedPreferences.edit();
              editor.putString(key, value);
              editor.commit();
        }

   }

我的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.mbpictures.fastorder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>  
        </activity>
        <activity android:name=".Start" android:label="@string/app_name" ></activity>

        <activity android:name=".Login"></activity>

    </application>
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

</manifest>

最后我的login.php在XAMPP中运行:

<?php
require("../v0.6/inc/connect.php");
if(isset($_SERVER["HTTP_JSON"])){
    $json = $_SERVER['HTTP_JSON'];
    $data = json_decode($json);

    $username = $data->username;
    $password = $data->password;

    mysql_select_db("forder_info");
    $sql = mysql_query("SELECT * FROM bedienungen WHERE bedienung = '".$username."'");
    if(mysql_num_rows($sql) == 0){
        if($_POST["pw1"] == $_POST["pw2"]){
            $sql_i = "INSERT INTO bedienungen (bedienung, password) VALUES ('".$username."', '".$password."')";
            $sql_createTable = "CREATE TABLE IF NOT EXISTS `".$username."` (
            `id` int(32) NOT NULL, `essen` text NOT NULL, `tisch` text NOT NULL, `kommentar` text NOT NULL, `bedienung` text NOT NULL, `art` text NOT NULL, `preis` text NOT NULL, `place` text NOT NULL, `time` text NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
            if(mysql_query($sql_i)){

                 mysql_select_db("forder_todruck");

                if(mysql_query($sql_createTable)){
                    echo "true";
                }

            }
            else{
                ?>
                405
                <?php   
            }
        }
        else{
            ?>
            406
            <?php   
        }
    }
    else{
        $sql_c = mysql_query("SELECT * FROM bedienungen WHERE bedienung='".$username."'");
        $out_c = mysql_fetch_assoc($sql_c);
        if($out_c["password"] == $password){

                ?>
                true408
                <?

        }
        else{
            ?>
            407
            <?php   
        }
    }
}
else{
    ?>404<?php
}

?>

修改 现在我将我的代码编辑为:

public class Login extends ActionBarActivity {
    public static final String PREFS_NAME = "Fastorder_Settings";
    final Context context = this;

    EditText username;
    EditText password;
    EditText password2;
    EditText ip;

    ProgressDialog dialog = null;

    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            final Button btn_login = (Button) findViewById(R.id.btn_login);
            username = (EditText) findViewById(R.id.login_username);
            password = (EditText) findViewById(R.id.login_password);
            password2 = (EditText) findViewById(R.id.login_password2);
            ip = (EditText) findViewById(R.id.login_ip);

            btn_login.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    dialog = ProgressDialog.show(Login.this, "", 
                            "Bitte warten...", true);
                     new Thread(new Runnable() {
                            public void run() {
                                login();                          
                            }
                          }).start();                   
                }

            });
        } 


        void login(){
            try{
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost(ip.getText().toString()+"/fastorder/android/login.php"); // make sure the url is correct.
                //add your data
                nameValuePairs = new ArrayList<NameValuePair>(2);
                // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
                nameValuePairs.add(new BasicNameValuePair("username", username.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
                nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim())); 
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                //Execute HTTP Post Request
                response=httpclient.execute(httppost);
                // edited by James from coderzheaven.. from here....
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                final String response = httpclient.execute(httppost, responseHandler);
                System.out.println("Response : " + response); 

                if(response.equalsIgnoreCase("true")){
                    //save Username in Preferences
                     savePreferences("username", username.getText().toString());
                    //save password in Preferences 
                     savePreferences("password", password.getText().toString());
                    //save password in Preferences 
                     savePreferences("ip", ip.getText().toString());


                    Intent i = new Intent(Login.this, Start.class);

                    startActivity(i);
                }
                else if(response.equalsIgnoreCase("405")){
                    Toast.makeText(getApplicationContext(), "Fehler beim Speichern", Toast.LENGTH_LONG).show();
                }
                else if(response.equalsIgnoreCase("406")){
                    Toast.makeText(getApplicationContext(), "Passwoerte nicht gleich!", Toast.LENGTH_LONG).show();
                }
                else if(response.equalsIgnoreCase("true408")){
                    //save Username in Preferences
                     savePreferences("username", username.getText().toString());
                    //save password in Preferences 
                     savePreferences("password", password.getText().toString());
                    //save password in Preferences 
                     savePreferences("ip", ip.getText().toString());

                    Toast.makeText(getApplicationContext(), "Wieder eingeloggt!", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(Login.this, Start.class);

                    startActivity(i);
                }
                else if(response.equalsIgnoreCase("407")){
                    Toast.makeText(getApplicationContext(), "Passwort falsch!", Toast.LENGTH_LONG).show();
                }
                else if(response.equalsIgnoreCase("404")){
                    Toast.makeText(getApplicationContext(), "Fehler beim abgleich!", Toast.LENGTH_LONG).show();
                }
                dialog.dismiss();

            }catch(Exception e){
                dialog.dismiss();
                Toast.makeText(getApplicationContext(), "Fehler beim Login!"+e.getMessage(), Toast.LENGTH_LONG).show();
                System.out.println("Exception : " + e.getMessage());
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.login, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.menu_login_help) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                    // set title
                    alertDialogBuilder.setTitle("Hilfe");

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("Tragen Sie ihre Bedienungsid und Ihr Passwort ein! \n"
                                + "Sollten Sie bereits registriert sein, werden sie eingeloggt\n"
                                + "andernfalls werden Sie registriert\n \n"
                                + "Die Server-IP wird Ihnen vom Admin mitgeteilt.")
                        .setCancelable(true)
                        ;

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        private void savePreferences(String key, String value) {
              SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
              Editor editor = sharedPreferences.edit();
              editor.putString(key, value);
              editor.commit();
        }
}

如果我运行ProgressDialog打开一小段时间然后,应用程序再次崩溃...任何想法......? 现在我收到这个错误: 目标主机不能为空,或者在参数中设置。 scheme = null,host = null,path = 192.168.178.XX / fastorder / android / login.php

1 个答案:

答案 0 :(得分:1)

您正在应用程序的主线程上运行与网络相关的代码。解决这个问题的方法是启动AsyncTask,然后再处理结果,再次回到主线程上。

还有其他选择,例如Handler,但我相信AsyncTasks可以让它更简单地与UI进行交互,我认为这就是你所需要的。

在链接I passed中,有一个简单文件下载的代码片段AsyncTask包含解决此问题所需的一切,请看一下。