1

在掌握了 AsyncTask 之后,我又遇到了一个障碍。使用 JSON(连接到我的本地 SQLServer,我想使用 IMEI 作为字符串参数来检索一个对象。我为我的 AsyncTask 调用设置了一个断点,它只是崩溃,甚至没有经过。每个移动设备的 IMEI 是存储在我的数据库中。

我实现了以下 AsyncTask 并希望在我的设备上运行它,而不是在模拟器上。

我怀疑这可能与我的设备有关,因为我的两个 Web 服务都在我的 PC 上运行。我还更改了 APN 设置中的代理和端口以与我的服务的 URI 一致,但它仍然没有响应。

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = telephonyManager.getDeviceId();
        new loadDevice().execute(IMEI);

这是我的 OnCreate 方法的代码片段。

这是 AsyncTask 类:

//AsyncTask to load get ManifestID from Device
    public class loadDevice extends AsyncTask<String, String, Manifests>
    {
        private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        InputStream inputStream = null;
        String theString = "";
        StringBuilder builder;

        protected void onPreExecute()
        {
            progressDialog.setMessage("Getting your assigned Manifest...");
            progressDialog.show();
            progressDialog.setOnCancelListener(new OnCancelListener()
            {
                public void onCancel(DialogInterface arg0)
                {
                    loadDevice.this.cancel(true);
                }
            });
        }

        @Override
        protected Manifests doInBackground(String... arg0)
        {
            try
            {
                //http get request
                HttpGet request = new HttpGet(CentralURI + "/loadDevice/" + IMEI);
                //set the hedear to get the data in JSON format
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");

                DefaultHttpClient client = new DefaultHttpClient();

                //get the response
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();

                //read content
                InputStream is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null)
                {
                    builder.append(line);
                }
                is.close(); 
                theString = builder.toString();

                JSONObject manifestJSON = new JSONObject(theString);
                JSONArray manifest = manifestJSON.getJSONArray("loadDeviceResult");

                for(int i = 1; i < manifest.length(); i++)
                {
                    JSONObject manObj = manifest.getJSONObject(i);
                    man.ManifestID = manObj.getInt("OriginalSOGManifestID");
                    man.DeviceManifestID = manObj.getInt("DeviceManifestID");
                    man.IMEI = manObj.getString("IMEI");
                    man.JobType = manObj.getString("prefix");
                    man.RegNo = manObj.getString("RegNo");
                    man.Complete = manObj.getBoolean("Complete");
                    man.DateComplete = manObj.getString("DateComplete");
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return man;
        }

        protected void onPostExecute(Manifests manifest)
        {
            //Manifests manifestToGet = new Manifests();
            //String manifestCode = manifestToGet.getJobType() + Integer.toString(manifestToGet.getManifestID());
            //new getManifestItems().execute(manifestCode);
        }

    }

我的其他 AsyncTasks 工作正常。然而,在我把这个野兽作为第一个运行的 AsyncTask 之后,其他的都没有受到打击。

这是依赖于第一个 AsyncTask 检索到的对象中的属性的 AsyncTask:-

//ASyncTask to get ManifestItem from each manifest
    public class getManifestItems extends AsyncTask<String, String, ArrayList<ManifestItemObj>>
    {
        private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        InputStream inputStream = null;
        String theString = "";
        StringBuilder builder;

        protected void onPreExecute()
        {
            progressDialog.setMessage("Loading manifests...");
            progressDialog.show();
            progressDialog.setOnCancelListener(new OnCancelListener()
            {
                public void onCancel(DialogInterface arg0)
                {
                    getManifestItems.this.cancel(true);
                }
            });
        }

        @Override
        protected ArrayList<ManifestItemObj> doInBackground(String... params)
        {
            Manifests assign = man;
            String manifestCode = assign.getJobType() + Integer.toString(assign.getManifestID());

            try
            {
                //http get request
                HttpGet request = new HttpGet(POD_URI + "/getJobs/" +  manifestCode);
                //set the hedear to get the data in JSON format
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");

                DefaultHttpClient client = new DefaultHttpClient();

                //get the response
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();

                //read content
                InputStream is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                                builder.append(line);
                        }
                is.close(); 
                theString = builder.toString();

                JSONObject jobsJSON = new JSONObject(theString);

                JSONArray jobs = jobsJSON.getJSONArray("getJobsResult");

               ManifestItemObj miDumb = new ManifestItemObj();
                miDumb.ManifestItemID = -1;
                miDumb.FKID = -1;
                miDumb.JobType = "--Please Select--";

                for(int i = 1; i < jobs.length(); i++)
                {
                    JSONObject mit = jobs.getJSONObject(i);
                    ManifestItemObj mi = new ManifestItemObj();
                    mi.ManifestItemID = mit.getInt("ManifestItemID");
                    mi.JobType = mit.getString("JobType");
                    mi.FKID = mit.getInt("FKID");

                    //ShipTo object
                    JSONObject stObj = mit.getJSONObject("ShipTo");

                    ShipTo sto = new ShipTo();
                    sto.ShipToId = stObj.getInt("ShipToId");
                    sto.ShipToName = stObj.getString("ShipToName");
                    sto.ShipToAddress1 = stObj.getString("ShipToAddress1");
                    sto.ShipToAddress2 = stObj.getString("ShipToAddress2");
                    sto.ShipToCity = stObj.getString("ShipToCity");
                    sto.ShipToPostcode = stObj.getString("ShipToPCode");
                    sto.ShipToState = stObj.getString("ShipToState");
                    mi.shipTo = sto;

                    jobList.add(mi);
                }
                jobList.add(0, miDumb);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return jobList;
        }

        protected void onPostExecute(ArrayList<ManifestItemObj> mio)
        {
            ManifestItemAdapter mia = new ManifestItemAdapter(MainActivity.this, android.R.layout.simple_spinner_item, jobList);
            this.progressDialog.dismiss();
            list_job.setAdapter(mia);

        }
    }

编辑:这是logcat。我怀疑 Button 被投射到 Spinner 上。绝对不是我的意图,但这就是它的意思:-

05-03 14:20:20.603: E/Trace(797): error opening trace file: No such file or directory (2)
05-03 14:20:21.204: D/AndroidRuntime(797): Shutting down VM
05-03 14:20:21.204: W/dalvikvm(797): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-03 14:20:21.224: E/AndroidRuntime(797): FATAL EXCEPTION: main
05-03 14:20:21.224: E/AndroidRuntime(797): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.signonglass/com.signonglass.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Spinner
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-03 14:20:21.224: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 14:20:21.224: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
05-03 14:20:21.224: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-03 14:20:21.224: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-03 14:20:21.224: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)
05-03 14:20:21.224: E/AndroidRuntime(797): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Spinner
05-03 14:20:21.224: E/AndroidRuntime(797):  at com.signonglass.MainActivity.onCreate(MainActivity.java:55)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.Activity.performCreate(Activity.java:5008)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-03 14:20:21.224: E/AndroidRuntime(797):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-03 14:20:21.224: E/AndroidRuntime(797):  ... 11 more
4

3 回答 3

1

如果您没有授予访问读取电话状态的权限,则将此行添加到AndroidManifest.xml( <uses-permission android:name="android.permission.READ_PHONE_STATE" />)。

于 2013-05-03T04:41:42.647 回答
1

可能是以下两件事之一:

  1. 向清单添加权限 <uses-permission android:name="android.permission.READ_PHONE_STATE"> (删除 '<' 之后的空格)
  2. 您可以在 MainActivity 的布局文件中声明一个 Button,但在启动 Button 时将其转换为 Spinner,反之亦然。
于 2013-05-03T05:09:32.570 回答
0

I've fixed the problem, after shuffling the code around. I have since removed the button since it was not necessary.

于 2013-05-07T00:44:18.660 回答