使用IMEI作为JSON服务参数(Using IMEI as a parameter for JSO

2019-10-18 02:27发布

抓的AsyncTask后,我遇到了另一道隆起。 使用JSON(连接到我的本地SQLServer的,我想检索使用IMEI作为字符串参数的对象。我已经设置断点以我的AsyncTask调用,它只是崩溃,甚至没有走过。每个手机的IMEI是存储在我的数据库。

我实施与运行它我的设备上,而不是在模拟器上的,希望下面的AsyncTask。

我怀疑这可能与我的设备,既是我的Web服务是我的电脑上运行。 我也改变了代理和端口在我的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的。 我怀疑按钮被强制转换为微调。 绝对不是我的本意但是这就是它的说法: -

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

Answer 1:

可能是这2件事情之一:

  1. 添加权限来体现<用途的许可机器人:名称=“android.permission.READ_PHONE_STATE”>(除去后空间“<”)
  2. 你可能会宣布在MainActivity的布局文件中的按钮,但将它转换为微调,同时启动按钮或反之亦然。


Answer 2:

如果你没有给权限访问读取手机状态,那么这行添加到AndroidManifest.xml<uses-permission android:name="android.permission.READ_PHONE_STATE" />



Answer 3:

我已经解决了这一问题,围绕洗牌后的代码。 因为我已经删除的按钮,因为这是没有必要的。



文章来源: Using IMEI as a parameter for JSON Service