gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:10:52 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com