1

我正在创建一个在线词典 Android 应用程序。我正在使用 JSON 来请求用户输入的单词的定义。每次单击搜索按钮时,此输入都会输入到变量“文本”中。然后将输入的单词附加到返回定义的 API 请求 URL 中......它存储在底部方法 OnPost Execute() 的变量“结果”中,然后我的 TextView 应该设置为此字符串。

因此,我将整个 JSON 和 HTTPrequest 代码放在 onClickLIstener 中,因为用户输入总是在每次都更改和请求,但是在“public JSONObject lastTweet(String word)”之后的“throws ClientProtocolException”处出现错误,错误是“语法错误在令牌上,删除这些令牌”我正在使用 Enclipse Indigo。

这是我的代码:

    public class Dictionary extends Activity {
    String finalresult;
    HttpClient client = new DefaultHttpClient();
    TextView ansa;
    JSONObject json;
    Button Search;
    EditText input; 
    String text;
    final static String URL = "http://api.wordnik.com/v4/word.json/";        
    final static String URL2 = "/definitions?api_key=<MY API KEY>"; 
    String fresult;
    Dictionary dic = new Dictionary();

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dictionary);
        ansa = (TextView) findViewById(R.id.ansa);
        input = (EditText) findViewById(R.id.input);
        Search = (Button) findViewById(R.id.search);

     Search.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                // TODO Auto-generated method stub
                text = input.getText().toString();

                public JSONObject lastTweet(String word) 
                        throws ClientProtocolException, IOException, JSONException{

                    new Read().execute("text");   
                    StringBuffer strBuff = new StringBuffer();
                     strBuff.append(URL);
                     strBuff.append(word);
                     strBuff.append(URL2);


                    HttpGet get = new HttpGet(strBuff.toString());
                    HttpResponse r = client.execute(get);
                    int status = r.getStatusLine().getStatusCode();
                    if (status == 200){
                        HttpEntity e = r.getEntity();
                        String data = EntityUtils.toString(e);
                        JSONArray timeline = new JSONArray(data);
                        JSONObject last = timeline.getJSONObject(0);
                        return last;

                    }else{
                        Toast.makeText(Dictionary.this, "error", Toast.LENGTH_LONG);
                        return null;

                    }

                }


                class Read extends AsyncTask<String, Integer, String>{

                    @Override
                    public String doInBackground(String... params) {
                        // TODO Auto-generated method stub
                        try {
                            json = lastTweet(text);
                            return json.getString(params[0]);
                        } catch (ClientProtocolException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        fresult = result;
                        // TODO Auto-generated method stub
                                    }
                }
                    ansa.setText(fresult);
                }

        });

     }  

    public JSONObject lastTweet(String word) 
            throws ClientProtocolException, IOException, JSONException{

        new Read().execute("text");   
        StringBuffer strBuff = new StringBuffer();
         strBuff.append(URL);
         strBuff.append(word);
         strBuff.append(URL2);


        HttpGet get = new HttpGet(strBuff.toString());
        HttpResponse r = client.execute(get);
        int status = r.getStatusLine().getStatusCode();
        if (status == 200){
            HttpEntity e = r.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray timeline = new JSONArray(data);
            JSONObject last = timeline.getJSONObject(0);
            return last;

        }else{
            Toast.makeText(Dictionary.this, "error", Toast.LENGTH_LONG);
            return null;

        }

    }


    class Read extends AsyncTask<String, Integer, String>{

        @Override
        public String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                json = lastTweet(text);
                return json.getString(params[0]);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            fresult = result;
            // TODO Auto-generated method stub
        }
    }

}

有什么建议么?

4

1 回答 1

1

您是否尝试将请求代码移动到另一个类?

于 2012-07-28T14:52:16.783 回答