2015-11-11 30 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();它为我工作。 –

相关问题