我正在使用他们的API在Active.com上进行搜索,目前我的搜索陷入了此错误:org.json.JSONException:字符0的输入结束。我是新手,感谢您的帮助。

主要活动

      private class SearchTask extends AsyncTask<String, Integer, String>
{
    ProgressDialog dialog;

        @Override
        protected void onPreExecute() {

            dialog = ProgressDialog.show(StayActiveActivity.this,"","Please Wait...");

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                String result = ActiveHelper.download(params [0]);
                return result;
            } catch (ApiException e) {
                e.printStackTrace();
                Log.e("alatta", "Problem making search request");
            }
            return "";
        }
        @Override
        protected void onPostExecute(String result) {

            try {
                JSONObject obj = new JSONObject(result);
                m_results = obj.getJSONArray("results");
                if (m_results == null || m_results.length() == 0)
                {
                    Toast.makeText(getApplicationContext(),
                            "No Results found for " + m_search_text.getText(),
                            Toast.LENGTH_LONG).show();

                }
                else
                    m_search_results.setAdapter(new JSONAdapter(getApplicationContext()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

private class JSONAdapter extends BaseAdapter
{
    public JSONAdapter(Context c){

    }

    public int getCount()
    {
    return  m_results.length();
        }

    public Object getItem(int arg0){
        return null;
    }

    public long getItemId(int pos){
        return pos;

    }

    public View getView(int pos, View convertView, ViewGroup parent) {
        View tv;
        TextView t;

        if (convertView == null)
            tv = m_inflater.inflate (R.layout.item, parent, false);
        else
            tv = convertView;
        try {
            /* For each entry in the ListView, we need to populate
             * its text and timestamp */
            t = (TextView) tv.findViewById(R.id.text);
            JSONObject obj = m_results.getJSONObject(pos);

            t.setText (obj.getString("event") + ": " +
                        obj.getString("text"));

            t = (TextView) tv.findViewById(R.id.created_at);
            t.setText (obj.getString("created_at"));
        } catch (JSONException e) {

            Log.e("alatta", e.getMessage());
        }
        return tv;
    }

   }


主动助手

 public class ActiveHelper {

private static final String ACTIVE_SEARCH = "http://api.amp.active.com/search?&v=json&api_key=rm4agnjv6k4m4tvpft95xvbn";
    private static final int HTTP_STATUS_OK = 200;
    private static byte[] buff = new byte[1024];



 public static class SearchException extends Exception {
        public SearchException (String msg)
        {
            super (msg);
        }

        public SearchException (String msg, Throwable thr)
        {
            super (msg, thr);
        }
 }
        protected static synchronized String download (String parm)
                throws SearchException
            {
                String url = ACTIVE_SEARCH+ parm;
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url);
                try {
                    HttpResponse response = client.execute(request);
                    StatusLine status = response.getStatusLine();
                    if (status.getStatusCode() != HTTP_STATUS_OK)
                        throw new SearchException("Invalid response from search.active.com" +
                                status.toString());
                    HttpEntity entity = response.getEntity();
                    InputStream ist = entity.getContent();
                    ByteArrayOutputStream content = new ByteArrayOutputStream();
                    int readCount = 0;
                    while ((readCount = ist.read(buff)) != -1)
                        content.write(buff, 0, readCount);
                    return new String (content.toByteArray());
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SearchException("Problem using the API", e);
                }
            }
        }


堆栈跟踪

06-11 15:01:27.569: E/alatta(7698): Problem making search request
06-11 15:01:27.569: W/System.err(7698): org.json.JSONException: End of input at character 0 of
06-11 15:01:27.569: W/System.err(7698):     at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
06-11 15:01:27.569: W/System.err(7698):     at org.json.JSONTokener.nextValue(JSONTokener.java:93)
06-11 15:01:27.569: W/System.err(7698):     at org.json.JSONObject.<init>(JSONObject.java:154)
06-11 15:01:27.569: W/System.err(7698):     at org.json.JSONObject.<init>(JSONObject.java:171)
06-11 15:01:27.569: W/System.err(7698):     at my.stayactive.plan.StayActiveActivity$SearchTask.onPostExecute(StayActiveActivity.java:120)
06-11 15:01:27.569: W/System.err(7698):     at my.stayactive.plan.StayActiveActivity$SearchTask.onPostExecute(StayActiveActivity.java:1)
06-11 15:01:27.569: W/System.err(7698):     at android.os.AsyncTask.finish(AsyncTask.java:417)
06-11 15:01:27.569: W/System.err(7698):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-11 15:01:27.579: W/System.err(7698):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-11 15:01:27.579: W/System.err(7698):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 15:01:27.579: W/System.err(7698):     at android.os.Looper.loop(Looper.java:150)
06-11 15:01:27.579: W/System.err(7698):     at android.app.ActivityThread.main(ActivityThread.java:4293)
06-11 15:01:27.579: W/System.err(7698):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 15:01:27.579: W/System.err(7698):     at java.lang.reflect.Method.invoke(Method.java:507)
06-11 15:01:27.579: W/System.err(7698):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-11 15:01:27.579: W/System.err(7698):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-11 15:01:27.579: W/System.err(7698):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

onPostExecute(...)中,我认为您需要使用_results作为JSONArray的名称,而不是results ...

m_results = obj.getJSONArray("results"); // Change to "_results"

关于java - JSONException故障排除:输入在字符0处结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10987902/

10-12 02:55