Android - 将参数传递给 Android 应用程序中的 RESTful URL
Android - Passing Params to the RESTful URL in Android app
我正在开发一个 android 应用程序,它涉及传递 parms.I 想要将参数传递给 android 中的 url。
如何在 Android 应用程序中使用 EditText 字段将参数传递给 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>
在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
}
现在在 doInBackground
中使用 st1
字符串来获取用户输入文本。
我正在开发一个 android 应用程序,它涉及传递 parms.I 想要将参数传递给 android 中的 url。
如何在 Android 应用程序中使用 EditText 字段将参数传递给 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>
在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
}
现在在 doInBackground
中使用 st1
字符串来获取用户输入文本。