我在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
你有什麼在你的logCat? –
NetworkOnMainThread異常。網絡調用需要進入一個不同的線程超越android 2.3 – Peshal
[見這裏](http://stackoverflow.com/questions/18388468/app-crashes-every-time-i-make-an-http-request/18388502# 18388502) – codeMagic