簡體   English   中英

Android - 將參數傳遞給 Android 應用程序中的 RESTful URL

[英]Android - Passing Params to the RESTful URL in Android app

我正在開發一個涉及傳遞參數的 android 應用程序。我想將參數傳遞給 android 中的 url。

如何使用 EditText 字段將參數傳遞到 Android 應用程序中的 RESTful URL。 還給我提供了在 android 中調用和傳遞參數的 get 和 post 方法 webservice 的基本示例。

主活動.java:

        public class MainActivity extends Activity implements OnClickListener {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.my_button).setOnClickListener(this);
        }
        @Override
        public void onClick(View arg0) {
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(false);
            new LongRunningGetIO().execute();
        }
        private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
            protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
               InputStream in = entity.getContent();
                 StringBuffer out = new StringBuffer();
                 int n = 1;
                 while (n>0) {
                     byte[] b = new byte[4096];
                     n =  in.read(b);
                     if (n>0) out.append(new String(b, 0, n));
                 }
                 return out.toString();
            }

            @Override
            protected String doInBackground(Void... params) {
                 HttpClient httpClient = new DefaultHttpClient();
                 HttpContext localContext = new BasicHttpContext();
                 String st1=null;
                 String st2=null;

                 EditText et1 = (EditText)findViewById(R.id.editText1);

                 EditText et2 = (EditText)findViewById(R.id.editText2);

                 st1= et1.getText().toString();
                 st2= et2.getText().toString();
                HttpGet httpGet = new HttpGet("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="+st1+"&ToCurrency="+st2);
                 Log.d("url", httpGet.toString());
                 Log.d("et1", et1.toString());
                 Log.d("et2", et2.toString());
                String text = null;
                 try {
                       HttpResponse response = httpClient.execute(httpGet, localContext);
                       HttpEntity entity = response.getEntity();
                       text = getASCIIContentFromEntity(entity);
                 } catch (Exception e) {
                     return e.getLocalizedMessage();
                 }
                 return text;
            }   

            protected void onPostExecute(String results) {
                if (results!=null) {
                    EditText et = (EditText)findViewById(R.id.my_edit);
                    et.setText(results);
                }
                Button b = (Button)findViewById(R.id.my_button);
                b.setClickable(true);
            }
        }
        }

    activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

         <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Http GET Demo"/>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>



        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="GET"
            android:id="@+id/my_button"/>
        <EditText 
            android:layout_margin="20dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:minLines="15"
            android:maxLines="15"
            android:textSize="12sp"
            android:editable="false"
            android:id="@+id/my_edit"/>
    </LinearLayout>

doInBackground有以下幾行:

....
st1= et1.getText().toString();
st2= et2.getText().toString();

導致問題,因為et1et1是 UI 元素,所以我們只能從 UI 線程而不是從任何后台線程訪問這些視圖,這些線程在doInBackground用於在單獨的線程中執行任務。

使用onPreExecute方法從EditText獲取數據:

String st1=null;
@Override
    protected void onPreExecute() {
        super.onPreExecute();
        EditText et1 = (EditText)findViewById(R.id.editText1);
        st1= et1.getText().toString();
        // do same for other
    } 

現在在doInBackground使用st1 String 來獲取用戶輸入文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM