Java/Android 应用程序意外关闭/POST 到服务器

标签 java android post https

我在 Eclipse 中没有任何错误或警告,而且我对 Android 编程完全陌生,所以我什至不知道从哪里开始。

我的应用程序只是一个简单的表单,我需要将其发布到在线 php 脚本。

这是我的主要 Activity 减去导入的大部分内容。我没有将其设置为执行任何返回值或任何操作,而且说实话,我什至不知道如果它确实在其他情况下工作会发生什么比数据将在我的数据库中..但 PHP 脚本甚至根本没有被我的应用程序调用。

根据我在 Google 中找到的内容,我尝试过 - 添加支持库 -将目标sdk版本从19更改为18

请问我做错了什么?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apply);

    subm = (Button) findViewById(R.id.button1);
    fname = (EditText) findViewById(R.id.editText1);
    lname = (EditText) findViewById(R.id.editText2);
    addr = (EditText) findViewById(R.id.editText3);
    city = (EditText) findViewById(R.id.editText4);
    state = (EditText) findViewById(R.id.editText5);
    zip = (EditText) findViewById(R.id.editText6);
    phone = (EditText) findViewById(R.id.editText7);
    dob = (EditText) findViewById(R.id.editText8);
    email = (EditText) findViewById(R.id.editText9);
    ssn = (EditText) findViewById(R.id.editText10);

    subm.setOnClickListener(
    new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try{

                String httpsURL = "https://example.com/apis/submit_credit_application.php";

                String query = "fname="+URLEncoder.encode(fname.getText().toString(),"UTF-8"); 
                query += "&lname="+URLEncoder.encode(lname.getText().toString(),"UTF-8");
                query += "&addr="+URLEncoder.encode(addr.getText().toString(),"UTF-8");
                query += "&city="+URLEncoder.encode(city.getText().toString(),"UTF-8");
                query += "&state="+URLEncoder.encode(state.getText().toString(),"UTF-8");
                query += "&zip="+URLEncoder.encode(zip.getText().toString(),"UTF-8");
                query += "&phone="+URLEncoder.encode(phone.getText().toString(),"UTF-8");
                query += "&dob="+URLEncoder.encode(dob.getText().toString(),"UTF-8");
                query += "&email="+URLEncoder.encode(email.getText().toString(),"UTF-8");
                query += "&ssn="+URLEncoder.encode(ssn.getText().toString(),"UTF-8");

                URL myurl = new URL(httpsURL);
                HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
                con.setRequestMethod("POST");

                con.setRequestProperty("Content-length", String.valueOf(query.length())); 
                con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
                con.setDoOutput(true); 
                con.setDoInput(true); 

                DataOutputStream output = new DataOutputStream(con.getOutputStream());  
                output.writeBytes(query);
                output.close();


            }catch(IOException e){

                Toast.makeText(
                    getApplicationContext(),
                    (CharSequence) e, 
                    Toast.LENGTH_LONG
                ).show();

            }

        }
    });
}

here's a Pastebin with my logcat

最佳答案

正如您的错误所示,您无法在主线程上运行网络任务。 AsyncTasks 非常适合运行您不想阻塞主线程的短任务。 链接到谷歌文档。 http://developer.android.com/reference/android/os/AsyncTask.html

// This class is just added somewhere in your main activity, like a function.
 private class PostFormTask extends AsyncTask<String, Integer, Long> {

     protected Long doInBackground(String... queryDetails) {

     try{

            String httpsURL = "https://example.com/apis/submit_credit_application.php";

            String query = "fname="+URLEncoder.encode(queryDetails[0],"UTF-8"); 
            query += "&lname="+URLEncoder.encode(queryDetails[1],"UTF-8");
            query += "&addr="+URLEncoder.encode(queryDetails[2],"UTF-8");
            // Keep adding to your query but instead of getting your details
            // from the textview they are in the queryDetails array.


            URL myurl = new URL(httpsURL);
            HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
            con.setRequestMethod("POST");

            con.setRequestProperty("Content-length", String.valueOf(query.length())); 
            con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
            con.setDoOutput(true); 
            con.setDoInput(true); 

            DataOutputStream output = new DataOutputStream(con.getOutputStream());  
            output.writeBytes(query);
            output.close();


        }catch(IOException e){

            Toast.makeText(
                getApplicationContext(),
                (CharSequence) e, 
                Toast.LENGTH_LONG
            ).show();

        }
         return false
     }

     protected void onPostExecute(Long result) {

     }
 }

然后在您的 onClick 事件上就可以了。

new PostFormTask().execute(fname.getText().toString(),
                                    lname.getText().toString() );
// just send your form details to your task here, you will want to add all your details 
// from your above code.

希望有帮助。

关于Java/Android 应用程序意外关闭/POST 到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897067/

相关文章:

apache - 没有获取 POST 方法请求参数

java - Gson - 它是如何工作的?

java - Spring MVC 显示日期变量

Android 滑动列表

android - android中的键码后退

C# HttpWebRequest/Response 给出 400 Bad Request。但 Firefox HTTP 资源测试则不然

Java 图形用户界面 |有序列表

Java预处理阶段

android - 使用 MediaRecorder.AudioSource.VOICE_COMMUNICATION 录制的音频在某些搭载 Android 10 的设备上为空

javascript - 跨(子)域 AJAX POST 请求(带文件/大体)