如何在android中解决Json tokener错误?

时间:2015-09-24 12:47:37

标签: android json android-json

我正在使用mysql来获取JSON中的数据,但是当我在url中传递值时,我得到JSON token error in Android。 我不知道我在哪里弄错了。

我的Logcat

09-24 08:35:03.513: E/AndroidRuntime(4276): Process: com.pitechnologies.progresscard, PID: 4276
09-24 08:35:03.513: E/AndroidRuntime(4276): java.lang.NullPointerException
09-24 08:35:03.513: E/AndroidRuntime(4276):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at org.json.JSONArray.<init>(JSONArray.java:92)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at org.json.JSONArray.<init>(JSONArray.java:108)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at com.pitechnologies.progresscard.Messages2.showList(Messages2.java:138)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at com.pitechnologies.progresscard.Messages2$1GetDataJSON.onPostExecute(Messages2.java:215)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at com.pitechnologies.progresscard.Messages2$1GetDataJSON.onPostExecute(Messages2.java:1)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.os.AsyncTask.finish(AsyncTask.java:632)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.os.Looper.loop(Looper.java:136)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at android.app.ActivityThread.main(ActivityThread.java:5001)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at java.lang.reflect.Method.invoke(Method.java:515)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-24 08:35:03.513: E/AndroidRuntime(4276):     at dalvik.system.NativeStart.main(Native Method)

MainActivity

public class Messages2 extends Activity {

    private String[] navMenuTitles;
    private TypedArray navMenuIcons;
    private EditText editTextName;
    SharedPreferences sp;
    private String jsonResult;
    private ListView listView;
    private Button b;
    EditText etname, et;
    TextView tv;
    String myJSON;

    private static final String TAG_RESULTS="result";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_NAME = "message_recd";
    private static final String TAG_ADD ="message_sent";

    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;

    ListView list;

    public static final String USER_NAME = "USERNAME";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InputStream is = null;
        // http post
        Intent intent1 = getIntent();
        String fName = intent1.getStringExtra("fname");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();    
        nameValuePairs.add(new BasicNameValuePair("username",fName));
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/progress_card/testing.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
            }



        setContentView(R.layout.activity_second);




        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
        // load icons from
                                                            // strings.xml



        b=(Button)findViewById(R.id.Button01);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();


       b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = getIntent();
                String fName = intent.getStringExtra("fname");

                   getData(fName);

            }
        });
    }


        protected void showList(){
            try {

                HashMap<String,String> person = new HashMap<String,String>();
                Intent intent1 = getIntent();
                String fName = intent1.getStringExtra("fname");
                person.put(TAG_NAME,fName);
                JSONArray peoples = new JSONArray(myJSON);
                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String name=null, address=null;
                    if(c.has("message_recd"))
                        name = c.getString("message_recd");
                    else if(c.has("message_sent"))
                        address = c.getString("message_sent");

                    HashMap<String,String> persons = new HashMap<String,String>();
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
                    personList.add(persons);
                }

                ListAdapter adapter = new SimpleAdapter(
                        Messages2.this, personList, R.layout.list_item,
                        new String[]{TAG_NAME,TAG_ADD},
                        new int[]{R.id.name, R.id.address}
                );

                list.setAdapter(adapter);

            } catch (JSONException e) {
                Log.i("tagconvertstr", "["+myJSON+"]");
            }
        }

        public void getData(String fName){
            class GetDataJSON extends AsyncTask<String, Void, String>{

                @Override
                protected String doInBackground(String... params) {
                    String paramUsername = params[0];
                    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                    HttpPost httppost = new HttpPost("http://10.0.2.2/progress_card/testing.php");

                    // Depends on your web service
                    httppost.setHeader("Content-type", "application/json");

                    InputStream inputStream = null;
                    String result = null;


                         Intent intent1 = getIntent();
                         String fName = intent1.getStringExtra("fname");
                         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();   
                         nameValuePairs.add(new BasicNameValuePair("username",fName));
                        try{
                            HttpClient httpclient1 = new DefaultHttpClient();
                            HttpPost httppost1 = new HttpPost("http://10.0.2.2/progress_card/testing.php");
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            HttpResponse response = httpclient.execute(httppost);
                            HttpEntity entity = response.getEntity();
                            InputStream is  = entity.getContent();
                        // json is UTF-8 by default
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                            sb.append(line + "\n");
                        }
                        result = sb.toString();
                    } catch (Exception e) {
                        Log.i("tagconvertstr", "["+result+"]");
                    }
                    finally {
                        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                    }
                    return result;
                }

                @Override
                protected void onPostExecute(String result){
                    myJSON=result;
                    showList();
                }
            }
            GetDataJSON g = new GetDataJSON();
            g.execute(fName);
        }

}

0 个答案:

没有答案