2014-02-20 69 views
0

我在Eclipse中沒有錯誤或警告,我完全不熟悉Android編程,所以我甚至不知道從哪裏開始。Java/Android應用程序意外關閉/ POSTing到服務器

我的應用程序只是一個簡單的表單,我需要在網上發佈到php腳本。

這裏是我的主要活動減去進口的大部分..我沒有設置做任何與返回值或任何東西,和TBH我甚至不知道如果它工作會發生什麼除了數據將在我的數據庫..但PHP腳本甚至沒有被我的應用程序調用。

基礎上的事情,我在谷歌發現,我已經試過 -Adding支持庫 - 改變從19目標SDK版本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

+0

你有什麼在你的logCat? –

+3

NetworkOnMainThread異常。網絡調用需要進入一個不同的線程超越android 2.3 – Peshal

+1

[見這裏](http://stackoverflow.com/questions/18388468/app-crashes-every-time-i-make-an-http-request/18388502# 18388502) – codeMagic

回答

1

由於您的錯誤提示您無法在主線程上運行網絡任務。 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. 

希望有所幫助。

1

好吧!因爲你想要你兩分。 Here是一個很好的教程,我在andorid中使用AsyncTask調用json Web服務。基本上AsyncTask創建一個可以進行網絡操作的新線程。

相關問題