3

URIから情報を受信して​​いるHTTPGETがあります。URIはGoogleショッピング用です。

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

(キーを外します)。

から変更できる方法はありますか

q=digital+camera

ユーザーがEditTextに入れるものに?

つまり、基本的には、EditTextでGoogleショッピングで検索される内容を変更したいと思います。

最初の画面、検索クエリ用のEditTextを含むProductSearchEntry:

ここに画像の説明を入力してください

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

次に、2番目のクラスである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();
    }
}
}

これは、HTTPGETで受信したコードに変更されたTextViewで構成されるProductSearchMethodクラスを参照します。

ここに画像の説明を入力してください

コード:

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は優れていますが、テキストが「LoadingItems」からWebサイトコードに変更されることはありません。以前は動作していましたが、検索対象を編集しようとしましたが(これはすべて^)、現在は変更されていません。

4

4 に答える 4

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();     
    }     
}     
}   
于 2012-04-14T10:29:34.273 に答える
0

http://androidforums.com/developer-101/528924-arduino-android-internet-garage-door-works-but-could-use-input.htmlをチェックしてください。Asynctaskを使用してローカルArduinoサーバーでgetコマンドをトリガーします。Arduinoのピン番号と、必要に応じて、URLの末尾にポート番号を追加します。私はあなたがあなたを助けるためにそれを使うことができると確信しています。

于 2012-04-13T07:15:42.130 に答える
0

ええ... getSearchData() メソッドを変更して、パラメーターとして文字列を含めます

public String getSearchData(String query) throws Exception{

次に、その文字列をクエリ URL に挿入し、スペースを「+」に置き換えます。たとえば、文字列を 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"

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

    }
}

最後に、クエリが実行中にアプリをフリーズさせないように、実行中の非同期タスクについて調べたいと思うでしょう。

于 2012-04-08T05:47:03.567 に答える
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");
于 2012-04-08T05:53:34.563 に答える