gpt4 book ai didi

java - 从 Android 应用程序向网站发布数据时的 android.os.NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-12-01 18:29:43 26 4
gpt4 key购买 nike

嗨,我开发了一个应用程序,可以将数据从 Android 发布到网站。现在,当我运行应用程序时,我收到消息不幸的是应用程序已停止然后 ap 关闭。从 Logcat 我发现它的 android.os.NetworkOnMainThreadException。我搜索它并研究这些东西来克服它http://developer.android.com/reference/android/os/AsyncTask.html

我尝试了很多修改代码,但出现了很多错误。请通过修改删除 android.os.NetworkOnMainThreadException。谢谢

带有 android.os.NetworkOnMainThreadException 的实际 java 代码是。

package com.latlongapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

Button sendButton;

EditText msgTextField;

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

// make message text field object
msgTextField = (EditText) findViewById(R.id.msgTextField);
// make send button object
sendButton = (Button) findViewById(R.id.sendButton);
}

public void send(View v)
{
// get the message from the message text box
String msg = msgTextField.getText().toString();

// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}

}

}

最佳答案

异常是因为您尝试进行网络操作,即在其主线程上发送数据。使用 AsyncTask 发送数据。

class SendTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// get the message from the message text box
String msg = msgTextField.getText().toString();

// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
return null;
}

在你的 send() 方法中使用这个

public void send(View v){
new SendTask.execute();
}

关于java - 从 Android 应用程序向网站发布数据时的 android.os.NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24831726/

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