gpt4 book ai didi

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

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

我正在尝试通过后台服务将数据发布到 Web 服务器。

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

unfortunately the program stopped working



这是我的代码:
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)

最佳答案

您不能在线程中使用任何与 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);
}

//主祭
<uses-permission android:name="android.permission.INTERNET"/>

关于android - 通过服务发布数据会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584315/

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