我正在尝试通过后台服务将数据发布到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)
答案 0 :(得分:3)
问题可能出在Toast上。
删除TOAST.show()。
答案 1 :(得分:3)
你不能在线程中使用任何与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"/>