2015-11-11 29 views
1

我正在開發一個涉及傳遞parms.I的android應用程序,我想在android中將參數傳遞給url。Android - 將Params傳遞給Android應用程序中的RESTful URL

如何使用EditText字段將parms傳遞到Android應用程序中的RESTful URL。同時給我一個get和post方法webservice調用和android中傳遞參數的基本示例。

MainActivity.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> 
+0

我得到錯誤,因爲不幸停住了。 –

+1

按照該答案的說法,您無法訪問doInBackground中的UI元素。使用onPreExecute。 – Ozgur

+0

@OzgurGUL。但上面的代碼正在爲我工​​作。早些時候,我將它寫爲st1 = et1.toString(); st2 = et2.toString();那裏我得到了錯誤,因爲不幸停住了。但現在將其更改爲st1 = et1.getText()。toString(); st2 = et2.getText()。toString();它爲我工作。 –

回答

1

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 
    } 

現在使用內部doInBackgroundst1字符串來獲取用戶輸入的文本。

+0

上面我的編碼工作很好。以前我寫它爲st1 = et1.toString(); st2 = et2.toString();那裏我得到了錯誤,因爲不幸停住了。但現在將其更改爲st1 = et1.getText()。toString(); st2 = et2.getText()。toString();它爲我工作。 –

相關問題