Java 有没有根据用户输入编辑URI的方法?
我有一个HTTP GET,它从URI接收信息。URI用于谷歌购物Java 有没有根据用户输入编辑URI的方法?,java,android,android-edittext,uri,Java,Android,Android Edittext,Uri,我有一个HTTP GET,它从URI接收信息。URI用于谷歌购物 https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom (我忘了带钥匙) 有什么办法可以改变它吗 q=digital+camera 用户在EditText中输入的任何内容 所以基本上,我希望EditText更改在Google购物上搜索的内容 htt
https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom
(我忘了带钥匙)
有什么办法可以改变它吗
q=digital+camera
用户在EditText中输入的任何内容
所以基本上,我希望EditText更改在Google购物上搜索的内容
https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom
第一个屏幕,ProductSearchEntry和EditText用于搜索查询:
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();
}
}
}
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类,该类由一个文本视图组成,该文本视图更改为HTTP GET中接收的代码:
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很好,但它不会将文本从“加载项目”更改为网站代码。我以前让它工作过,但后来我尝试编辑它搜索的内容(所有这些),现在它没有改变。是的。。。更改getSearchData()方法以包含字符串作为参数
公共字符串getSearchData(字符串查询)引发异常{
然后,将该字符串插入查询URL,将空格替换为“+”。您可能需要对该字符串进行进一步调整,例如对其进行URL编码
urisite=新的URI(“https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=“+query.replace”(“,“+”)+“&alt=atom”);
在XML中,创建包含以下行的按钮:
android:onClick=“search”
在ProductSearch活动中,添加以下方法,并将onCreate中的代码移到其中。您还需要在XML中创建EditText以供输入
public void search(View v)
{
EditText searchQuery = (EditText) findViewById(R.id.searchQuery);
ProductSearchMethod test = new ProductSearchMethod();
String returned;
try {
returned = test.getSearchData(searchQuery.getText().toString());
httpStuff.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最后,您可能希望了解运行异步任务的情况,以便查询不会在执行时冻结您的应用程序。可能是我搞错了,但为什么不将其作为参数传入呢
getSearchData() => getSearchData(string query)
然后你可以换线
URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom");
到
请检查我使用Asynctask在本地Arduino服务器上触发get命令。它会在URL末尾附加Arduino的pin码,并根据需要添加端口号。我相信您可以使用它来帮助您解决问题。对代码进行如下更改:
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);
}
});
}
}
还有像这样的活动,
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();
}
}
}
UTF-8是做什么的?我的意思是我该如何使用它?增加了赏金并完全改变了OP。是的!我修好了。非常感谢!