我正在开发一个涉及传递parms的android应用程序。我想在android中将参数传递给url。
如何使用EditText字段将parms传递到Android应用中的RESTful URL。另外给我一个get和post方法webservice调用和在android中传递parms的基本示例。
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
中有以下几行:
....
st1= et1.getText().toString();
st2= et2.getText().toString();
导致问题,因为et1
和et1
是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
}
现在在st1
中使用doInBackground
字符串来获取用户输入文本。