基本上,我想发送GET请求以获取表单,并根据表单的值发送POST请求,例如计算器。
<form action="/calc" method="POST">
<input id="sum" name="sum" value="">
<input id="num1" name="num1" value="6">
<input id="num2" name="num2" value="54">
<input type="submit" >
</form>
每次在Android中使用num1
发送GET请求时,num2
和HttpUrlConnection
的值都会更改。
如何获取HTML元素的值,在Android应用中计算总和,然后通过POST请求发送总和值以提交表单?
这是我的android代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MYAPP";
int rs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://local....";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.e(TAG,response);
//Process the response, get value of num1 and num2 and compute the sum;
rs = sum;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Exception");
}
});
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.e(TAG, response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.e(TAG, "Error");
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("sum", String.valueOf(rs));
return params;
}
};
queue.add(stringRequest);
queue.add(postRequest);
}
}