dongmeiyi2266 2013-06-18 21:59
浏览 90
已采纳

JSON错误:字符0的输入结束,然后工作?

I am receiving two different errors one on the first run, one on the second. I haven't seen this before and I honestly don't know enough to say what's going on. I'll show the code, then the errors, and then the lines the errors are pointing to.

private static final String TAG_SUCCESS = "success";
private static final String TAG_STORE = "store_info";
private static final String TAG_NUMBER = "store_num";
private static final String TAG_ADDRESS = "address";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_ZIP = "zip";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_basic_info, container, false);
        ab = getActivity().getActionBar();

        store_model = new Store_Model();
                ...
}

class Store extends AsyncTask<String, Void, String> {
        private ProgressDialog pDialog;
        JSONParser jParser = new JSONParser();
        JSONArray store_info = null;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading questions. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            // getting JSON string from URL
            String componentName = (String) ab.getSelectedTab().getText();
            companyName = Store_Fragment.model.getcName();
            projectName = Store_Fragment.model.getpName();
            storeNum = store.getText().toString();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("company", companyName));
            nameValuePairs.add(new BasicNameValuePair("project", projectName));
            nameValuePairs.add(new BasicNameValuePair("component",
                    componentName));
            nameValuePairs.add(new BasicNameValuePair("store", storeNum));

            JSONObject json = jParser.makeHttpRequest(url, "POST",
                    nameValuePairs);
            // Check your log cat for JSON response
            //Log.d("All Store Info: ", json.toString());
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    Log.v("RESPONSE", "Success!");
                    // products found: getting Array of Store Info
                    store_info = json.getJSONArray(TAG_STORE);

                    // looping through Store Info
                    for (int i = 0; i < store_info.length(); i++) {

                        JSONObject c = store_info.getJSONObject(i);

                        // Storing each JSON item in variable
                        String number = c.getString(TAG_NUMBER);
                        String address = c.getString(TAG_ADDRESS);
                        String city = c.getString(TAG_CITY);
                        String state = c.getString(TAG_STATE);
                        String zip = c.getString(TAG_ZIP);

                        // creating new HashMap
                        map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NUMBER, number);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_CITY, city);
                        map.put(TAG_STATE, state);
                        map.put(TAG_ZIP, zip);                      
                    }

                } else if (success == 0) {
                    // no questions found
                    Log.v("ERROR", "No JSON for you!");
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            error.setText("Invalid Store Number");
                            error.setBackgroundColor(getResources().getColor(
                                    R.color.red_bg));
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String string) {
            // dismiss the dialog
            pDialog.dismiss();

                store_model.setStoreNum(map.get(TAG_NUMBER));
                store_model.setAddress(map.get(TAG_ADDRESS));
                store_model.setCity(map.get(TAG_CITY));
                store_model.setState(map.get(TAG_STATE));
                store_model.setZip(map.get(TAG_ZIP));


            Dialog_Fragment df = new Dialog_Fragment();
            df.show(getFragmentManager(), "store");
        }
    };

So the first time I run this it has trouble with the JSON and gives me this error.

06-18 17:21:17.870: E/json data(23746): json result 
06-18 17:21:17.870: E/JSON Parser(23746): Error parsing data org.json.JSONException: End of input at character 0 of 

Then if I don't compile the code again, but just launch the app again from my tablet it gets to this screen and gets the JSON response, but gives me a null pointer.

Here is the JSON response and both logcats.

JSON

{
  "store_info":
   [
    {
      "store_num":"00607",
      "address":"790 W BROADWAY RD",
      "city":"TEMPE",
      "state":"AZ",
      "zip":"85282"
    }
   ],
   "success":1
}

1st Run Logcat

06-18 17:21:17.870: E/json data(23746): json result 
06-18 17:21:17.870: E/JSON Parser(23746): Error parsing data org.json.JSONException: End of input at character 0 of 
06-18 17:21:17.870: W/System.err(23746): java.lang.NullPointerException
06-18 17:21:17.870: W/System.err(23746):    at com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:105)
06-18 17:21:17.870: W/System.err(23746):    at com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:1)
06-18 17:21:17.870: W/System.err(23746):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-18 17:21:17.870: W/System.err(23746):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-18 17:21:17.870: W/System.err(23746):    at java.lang.Thread.run(Thread.java:856)
06-18 17:21:17.890: D/AndroidRuntime(23746): Shutting down VM
06-18 17:21:17.890: W/dalvikvm(23746): threadid=1: thread exiting with uncaught exception (group=0x41093930)
06-18 17:21:17.900: E/AndroidRuntime(23746): FATAL EXCEPTION: main
06-18 17:21:17.900: E/AndroidRuntime(23746): java.lang.NullPointerException
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:156)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:1)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask.finish(AsyncTask.java:631)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.Looper.loop(Looper.java:137)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at dalvik.system.NativeStart.main(Native Method)

2nd Run Logcat

06-18 17:22:19.910: E/json data(23841): json result {"store_info":[{"store_num":"00607","address":"790 W BROADWAY RD","city":"TEMPE","state":"AZ","zip":"85282"}],"success":1}
06-18 17:22:19.910: V/RESPONSE(23841): Success!
06-18 17:22:19.930: D/AndroidRuntime(23841): Shutting down VM
06-18 17:22:19.930: W/dalvikvm(23841): threadid=1: thread exiting with uncaught exception (group=0x41093930)
06-18 17:22:19.940: E/AndroidRuntime(23841): FATAL EXCEPTION: main
06-18 17:22:19.940: E/AndroidRuntime(23841): java.lang.NullPointerException
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:39)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Handler.handleCallback(Handler.java:725)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Looper.loop(Looper.java:137)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at dalvik.system.NativeStart.main(Native Method)

So now the lines I assume these errors are pointing to.

Error Log 1

com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:105)

int success = json.getInt(TAG_SUCCESS);

com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:156)

store_model.setStoreNum(map.get(TAG_NUMBER));

These errors seem self explanatory. You can't see if it was successful with the TAG_SUCCESS so therefor it crashes when trying to setStoreNum because that value is null. However when I run it the 2nd time it just works? That doesn't seem to make sense to me. It also then gives me this error.

Error Log 2

com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:39)

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder storeD = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    if (BasicInfo_Fragment.store_model != null) {

        mStore = BasicInfo_Fragment.store_model.getStoreNum();
        ...

        store = (TextView) getActivity().findViewById(R.id.store_d);
        ...

        store.setText("Store #: " + mStore); <-- LINE 39
        ...

    }

Error Log 2 seems to be getting to my Dialog Fragment and then crashing when it tries to create. It's getting a null pointer exception from the mStore variable as marked but it's supposed to be checking for null before it even starts, very frustrating.

So I know this is a pretty lengthy post but I was trying to supply all the info and make this an easy read.

Please help me figure out why this is not getting my JSON the first time. Then all of a sudden working if I don't compile again but crashing with a null pointer on the onCreate for my Dialog_Fragment.

PHP just for good measure so you can see how the JSON is being created

<?php

require 'connect.php';
require 'mysql_queries.php';

if ($query_run = mysql_query($store_info_query)) {
    if (mysql_num_rows($query_run) == NULL) {
        $response["success"] = 0;          
        echo json_encode($response);

    } else {
        $response ['store_info'] = array();
        while ($row = mysql_fetch_assoc($query_run)) {
            $info = array();
            $info['store_num'] = $row['column1'];
            $info ['address'] = $row['column2'];
            $info ['city'] = $row['column3'];   
            $info ['state'] = $row['column4'];
            $info ['zip'] = $row['column5'];        

            array_push($response["store_info"], $info);                     
        }
        // success
        $response["success"] = 1; 
        // echoing JSON response
        echo json_encode($response);        
    } 
} 

?>
  • 写回答

1条回答 默认 最新

  • doukuang6795 2013-06-18 22:18
    关注

    wich library do you use ? pls use the specific android libs

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject; 
    ... 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。