-1

我试图让一个应用程序看起来在一个 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));
        }

    }

这里是 Service 活动。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 android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.text.Html;
import android.util.Log;
import android.widget.ListView;
import android.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;
            }

    }


}

我对这段代码的目标是在吐司上显示一些 php 数据,但它没有......我不知道为什么,请帮帮我!

非常感谢!

4

1 回答 1

1

我认为 usingInentService更适合您要完成的工作。它在一个单独的 上运行Thread,完成后它会自行停止。

于 2013-09-22T03:09:33.787 回答