当前位置: 首页 > 知识库问答 >
问题:

通过服务发布数据会使应用程序崩溃

陆建木
2023-03-14

我正试图通过后台服务将数据发布到web服务器。

函数PostData将数据发送到服务器,但当我添加它时,程序显示:

不幸的是,程序停止工作

这是我的代码:

package com.yahya.trackingsysem;
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.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

    Handler handler=new Handler();
    String info;

    @Override
    public void onCreate() {
hello();
    }

    void hello(){

        final Runnable r = new Runnable()
        {
            public void run() 
            {
                Toast.makeText(getApplicationContext(), "Starting service", Toast.LENGTH_LONG).show();
           ////////////////////
                postData();
           ////////////////////
                handler.postDelayed(this, 10000);
            }
        };

        handler.postDelayed(r, 10000);

    }   

    //********************* فنكشن الارسال
    public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.webserver.net/tracker.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("name", "Hi"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
          httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } 
    //****************************************



    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


}

和堆栈跟踪:

10-27 06:11:59.094: E/AndroidRuntime(810): FATAL EXCEPTION: main
10-27 06:11:59.094: E/AndroidRuntime(810): android.os.NetworkOnMainThreadException
10-27 06:11:59.094: E/AndroidRuntime(810):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
10-27 06:11:59.094: E/AndroidRuntime(810):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-27 06:11:59.094: E/AndroidRuntime(810):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-27 06:11:59.094: E/AndroidRuntime(810):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-27 06:11:59.094: E/AndroidRuntime(810):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-27 06:11:59.094: E/AndroidRuntime(810):  at com.yahya.trackingsysem.MyService.postData(MyService.java:60)
10-27 06:11:59.094: E/AndroidRuntime(810):  at com.yahya.trackingsysem.MyService$1.run(MyService.java:37)
10-27 06:11:59.094: E/AndroidRuntime(810):  at android.os.Handler.handleCallback(Handler.java:730)
10-27 06:11:59.094: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 06:11:59.094: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:137)
10-27 06:11:59.094: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 06:11:59.094: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
10-27 06:11:59.094: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:525)
10-27 06:11:59.094: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 06:11:59.094: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 06:11:59.094: E/AndroidRuntime(810):  at dalvik.system.NativeStart.main(Native Method)
10-27 06:12:09.094: I/Process(810): Sending signal. PID: 810 SIG: 9
10-27 06:12:24.545: D/AndroidRuntime(828): Shutting down VM
10-27 06:12:24.545: W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-27 06:12:24.595: E/AndroidRuntime(828): FATAL EXCEPTION: main
10-27 06:12:24.595: E/AndroidRuntime(828): android.os.NetworkOnMainThreadException
10-27 06:12:24.595: E/AndroidRuntime(828):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
10-27 06:12:24.595: E/AndroidRuntime(828):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-27 06:12:24.595: E/AndroidRuntime(828):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-27 06:12:24.595: E/AndroidRuntime(828):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-27 06:12:24.595: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-27 06:12:24.595: E/AndroidRuntime(828):  at com.yahya.trackingsysem.MyService.postData(MyService.java:60)
10-27 06:12:24.595: E/AndroidRuntime(828):  at com.yahya.trackingsysem.MyService$1.run(MyService.java:37)
10-27 06:12:24.595: E/AndroidRuntime(828):  at android.os.Handler.handleCallback(Handler.java:730)
10-27 06:12:24.595: E/AndroidRuntime(828):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 06:12:24.595: E/AndroidRuntime(828):  at android.os.Looper.loop(Looper.java:137)
10-27 06:12:24.595: E/AndroidRuntime(828):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 06:12:24.595: E/AndroidRuntime(828):  at java.lang.reflect.Method.invokeNative(Native Method)
10-27 06:12:24.595: E/AndroidRuntime(828):  at java.lang.reflect.Method.invoke(Method.java:525)
10-27 06:12:24.595: E/AndroidRuntime(828):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 06:12:24.595: E/AndroidRuntime(828):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 06:12:24.595: E/AndroidRuntime(828):  at dalvik.system.NativeStart.main(Native Method)

共有2个答案

鲁龙野
2023-03-14

问题可能出在烤面包上。

取出烤面包。show()。

范福
2023-03-14

你不能在线程中使用任何与UI相关的东西,在这里,你在一个线程中显示Toast消息,这就是它崩溃的原因。为此,您必须使用处理程序来显示toast消息。

实例

Runnable runnable = new Runnable() {
            public void run() {
                Message msg = handler.obtainMessage();
                Bundle bundle = new Bundle();                   
                bundle.putString("myKey", dateString);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
      };

private final Handler handler = new Handler() {

      public void handleMessage(Message msg) {

          String aResponse = msg.getData().getString("message");

          Toast.makeText(getApplicationContext(), ""+msg, Toast.LENGTH_LONG).show();

          }
};

//在服务类上编写此代码。

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

//Mainfest

<uses-permission android:name="android.permission.INTERNET"/>
 类似资料:
  • 我正试图通过我的android应用程序发布一条推特。在网上阅读教程,我写了下面提到的代码。 但是,每次调用twitter.updatestatus()时,我都会遇到以下异常: MainActivity.java:

  • 当我在除我的计算机之外的任何计算机上运行此代码的发行版时,我得到“已经停止工作”错误:使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.Drawing;使用System.LINQ;使用System.Text;使用System.Threading.Tasks;使用System.Window

  • 问题内容: 我想构建一个从服务器获取数据的应用程序,并将其显示在xaml中。我是通用程序和Windows存储应用程序中的新手。我还没有发现任何有用的东西,因为ms sql解决方案需要通用存储应用程序不允许的库:的答案没有回答我的问题,但是Jeffery的回答做了。 问题答案: 您可以直接从Windows应用程序连接到MS SQL Server。常见的方法是托管数据服务,然后应用通过REST API

  • 我已经编写了一个混合应用程序,它使用react原生svg在react原生视图中呈现svg图像。一切都很完美。今天,当我为我的应用程序创建release apk并将其部署到模拟器时,它崩溃了,出现了一个错误: 只有当我从proguard规则中注释时,我才会看到这个问题。专业文件。我想混淆我的apk代码(我无法控制)。我使用的proguard文件与本例中显示的类似,https://github.com