2012-04-08 46 views
3

我有一个HTTP GET从URI接收信息。该URI适用于Google购物。任何方式来编辑基于用户输入的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购物上搜索的内容。

第一个屏幕,ProductSearchEntry与EditText上的搜索查询:

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); 
     } 
    }); 
    } 
} 

然后,我有一个第二类,产品搜索,没有图片,但只是这个代码:

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(); 
    } 
} 
} 

它引用其中包括被改为在HTTP GET收到的代码一个TextView的ProductSearchMethod类:

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来很好,但它不会将文本从“加载项目”更改为网站代码。我之前有过这样的工作,但后来我试图编辑它搜索的内容(所有这些^),现在它不会改变。

回答

1

进行更改,在你的代码一样

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();  
    }  
}  
} 
+0

是的!我修好了它。非常感谢! – Cole 2012-04-15 19:37:16

0

呀...更改getSearchData()方法中包含一个字符串作为参数

public String getSearchData(String query) throws Exception{

然后插入串入查询网址,用“+”代替空格。您可能需要对字符串进行进一步的调整,例如URL编码。

URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q="+query.replace(" ","+")+"&alt=atom");

在你的XML,创建一个包含以下行的按钮:

android:onClick="search"

在你的产品搜索活动,添加下面的方法,并在的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(); 

    } 
} 

最后,您可能需要阅读正在运行的异步任务,以便查询在执行时不会冻结您的应用。

+0

编辑OP与更新的代码。 – Cole 2012-04-10 00:31:08

+0

增加赏金,完全改变了OP。 – Cole 2012-04-11 01:44:57

0

可能是我把你错了,但你为什么不只是把它作为一个参数

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"); 

改变

URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=+ URLEncoder.encode(query, "UTF-8")+&alt=atom"); 
+0

UTF-8是做什么的?我的意思是我该如何使用它? – Cole 2012-04-10 22:10:51

+0

UTF-8是文本编码。 – 2012-04-11 04:08:57