gpt4 book ai didi

java - 如何嵌入 Internet 请求代码并获得在一个 OnClickListener 中实现的结果

转载 作者:行者123 更新时间:2023-11-30 04:05:48 24 4
gpt4 key购买 nike

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

因此,我将整个 JSON 和 HTTPrequest 代码放在 onClickLIstener 中,因为用户输入每次都会更改和请求,但我在“public JSONObject lastTweet(String word)”之后的“throws ClientProtocolException”处收到错误错误是“ token 语法错误,删除这些 token ”我正在使用 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
}
}

}

有什么建议吗?

最佳答案

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

关于java - 如何嵌入 Internet 请求代码并获得在一个 OnClickListener 中实现的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com