gpt4 book ai didi

java - 有什么方法可以根据用户输入编辑 URI?

转载 作者:太空狗 更新时间:2023-10-29 14:27:57 25 4
gpt4 key购买 nike

我有一个从 URI 接收信息的 HTTP GET。 URI 用于 Google 购物。

https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom

(遗漏了我的 key )。

有什么方法可以改变它

q=digital+camera

用户在 EditText 中输入的任何内容?

基本上,我希望 EditText 更改在 Google 购物上搜索的内容。

第一个屏幕,带有用于搜索查询的 EditText 的 ProductSearchEntry:

enter image description here

ProductSearchEntry 代码

public class ProductSearchEntry extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.productsearchentry);

Button search = (Button) findViewById(R.id.searchButton);

search.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent searchIntent = new Intent(getApplicationContext(), ProductSearch.class);
startActivity(searchIntent);
}
});
}
}

然后,我有第二个类,ProductSearch,没有图片,只有这段代码:

public class ProductSearch extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.productsearchresults);
EditText searchQuery = (EditText) findViewById(R.id.searchQuery);

ProductSearchMethod test = new ProductSearchMethod();
String entry;
TextView httpStuff = (TextView) findViewById(R.id.httpTextView);
try {
entry = test.getSearchData(searchQuery.getText().toString());
httpStuff.setText(entry);
} catch (Exception e) {
e.printStackTrace();
}
}
}

它引用 ProductSearchMethod 类,该类由一个 TextView 组成,该 TextView 已更改为在 HTTP GET 中收到的代码:

enter image description here

代码:

public class ProductSearchMethod {

public String getSearchData(String query) throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q="+query.replace(" ","+")+"&alt=atom");
HttpGet request = new HttpGet();
request.setURI(site);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally{
if (in != null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

ProductSearchMethod 运行良好,但它不会将文本从“加载项”更改为网站代码。我之前让它工作,但后来我尝试编辑它搜索的内容(所有这些 ^),现在它没有改变。

最佳答案

像这样修改你的代码

public class ProductSearchEntry extends Activity{ 
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.productsearchentry);
EditText etSearch = (EditText) findViewById(id of your edittext);
Button search = (Button) findViewById(R.id.searchButton);

search.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//while calling intent

Intent searchIntent = new Intent(getApplicationContext(), ProductSearch.class);
searchIntent.putExtra("searchText",etSearch.getText().toString());
startActivity(searchIntent);
}
});
}
}

和另一个类似的 Activity ,

public class ProductSearch extends Activity{     
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.productsearchresults);
String searchQuery = getIntent().getStringExtra("searchText");
ProductSearchMethod test = new ProductSearchMethod();
String entry;
TextView httpStuff = (TextView) findViewById(R.id.httpTextView);
try {
entry = test.getSearchData(searchQuery);
httpStuff.setText(entry);
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于java - 有什么方法可以根据用户输入编辑 URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060582/

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