有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图在服务中运行Asynctask,eclipse看起来不错,但应用程序崩溃了,这里出了什么问题?

我想让一个应用程序在JSON编码的php文件上运行,没问题。所有这些都运行得很好,但现在我正试图将该类放到一个服务中,因为我想在后台反复运行它,但应用程序会崩溃,(在电话和模拟中)

有人能告诉我这里出了什么问题吗

这是密码

首先是主要活动。爪哇

[...]IMPORTS  
  public class ServiceMainActivity extends Activity {

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

        public void onClickComenzar(View v) {
            startService(new Intent(getBaseContext(), MyService.class));
        }

        public void onClickDetener(View v) {
            stopService(new Intent(getBaseContext(), MyService.class));
        }

    }

这里是服务活动。java,我试图将我的异步任务放在其中的地方

package attempt.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.os.AsyncTask;
import 安卓.os.IBinder;
import 安卓.text.Html;
import 安卓.util.Log;
import 安卓.widget.ListView;
import 安卓.widget.Toast;

public class MyService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        try {


            new DoBackgroundTask().execute("http://iatek.eu/sys/getsmsx.php");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(getBaseContext(), "service stoped",
                Toast.LENGTH_SHORT).show();
    }

    private class DoBackgroundTask extends AsyncTask<String, String, JSONObject> {

        @Override
         protected JSONObject doInBackground(String... urls) {
             JSONObject obj = null;
             try{

                 HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(urls[0]);
                   // HttpResponse response = httpclient.execute(httppost);  

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

                    String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
                    obj = new JSONObject(jsonResult);

                } 
                catch (JSONException e) {
                    e.printStackTrace();
                } 
                catch (ClientProtocolException e) {
                    e.printStackTrace();
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }

                return obj;

             }

        protected void onProgressUpdate( ) {
            // TODO Auto-generated method stub

        }

        @Override
          protected void onPostExecute(JSONObject obj) {
               JSONArray jsonArray;
               String dana = null;
                try {

                     jsonArray = obj.getJSONArray("posts");
                     JSONObject childJSONObject = jsonArray.getJSONObject(1);
                     String sms = childJSONObject.getString("sms");
                    Toast.makeText(getBaseContext(), "sms"+sms,
                            Toast.LENGTH_SHORT).show();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         stopSelf();
         }
        public StringBuilder inputStreamToString(InputStream is) {
             String rLine = "";
             StringBuilder answer = new StringBuilder();
             BufferedReader rd = new BufferedReader(new InputStreamReader(is));

             try {
              while ((rLine = rd.readLine()) != null) {
               answer.append(rLine);
                }
             }

             catch (IOException e) {
                 e.printStackTrace();
              }
             return answer;
            }

    }


}

我对这段代码的目标是在toast上显示一些php数据,但它不。。。我不知道为什么,请帮帮我

非常感谢


共 (1) 个答案

  1. # 1 楼答案

    我认为使用InentService更适合于您试图实现的目标。它在一个单独的Thread上运行,完成后会自动停止