2012-07-28 51 views
1

我正在創建在線詞典Android應用程序。 我正在使用JSON來請求用戶輸入的單詞的定義。 每次單擊搜索按鈕時,此輸入都會進入變量「文本」。 然後,輸入的字被添加到API請求URL中,該URL請求URL返回定義......存儲在變量「result」中的底部方法OnPost Execute() 然後我的TextView應該被設置爲這個String。如何嵌入互聯網請求代碼並獲得在一個OnClickListener中實現的結果

因此,將整個JSON和HTTPrequest代碼放在onClickLIstener中,因爲用戶輸入總是隨時更改和請求,但即使在「public JSONObject lastTweet(String word)」錯誤之後出現「拋出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 
     } 
    } 

} 

有什麼建議?

回答

1

您是否試過將請求代碼移動到另一個類中?

+0

我做了,但我不斷收到StackOverflow錯誤,應用程序無法正常工作。 – user1559736 2012-07-28 14:53:11

相關問題