Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序在访问本地主机时崩溃_Java_Php_Android_Post_Login - Fatal编程技术网

Java Android应用程序在访问本地主机时崩溃

Java Android应用程序在访问本地主机时崩溃,java,php,android,post,login,Java,Php,Android,Post,Login,对不起,我英语不好。。这是我的第一个帖子:D 但是我在Android/Java编程方面有一个大问题,我对Android编程非常陌生 我尝试将数据发送到外部PHP服务器(在同一网络中),并将其与SharedReferences一起保存。但如果我按下“登录”按钮,应用程序总是崩溃 这是我的密码: Login.class public class Login extends ActionBarActivity { final Context context = this; @O

对不起,我英语不好。。这是我的第一个帖子:D 但是我在Android/Java编程方面有一个大问题,我对Android编程非常陌生

我尝试将数据发送到外部PHP服务器(在同一网络中),并将其与SharedReferences一起保存。但如果我按下“登录”按钮,应用程序总是崩溃

这是我的密码: 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();
        }

   }
My 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>

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

<?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
}

?>

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

还有其他的替代方案,如的,但我相信AsyncTasks使与UI的交互更加简单,我认为这正是您所需要的


在链接I中,有一个简单的文件下载异步任务的代码片段,其中包含修复此问题所需的所有内容,请查看。

请发布崩溃日志。您使用的本地主机ip地址是什么?我猜
NetworkMainThreadException
会导致应用程序崩溃,只要Android在自己的操作系统上运行,试图通过模拟器或手机访问它是没有意义的。Localhost必须托管在android上才能访问。否则,它将查找操作系统上不可用的内容。试着使用一个免费的主机/域名,它在任何地方都可以使用,看看它是否有效。你在ip字段中键入了什么?请查看我的新代码(在编辑下)。你的意思是这样吗?检查一下:它使用异步任务。如果它有效,则根据您的需要更改并采用它。谷歌搜索它,您将发现更多:
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();
        }
}