我正在开发一个涉及解析的android应用程序。我想在android中将参数传递给url。我该怎么办?
答案 0 :(得分:6)
请试一试。
HttpPost postMethod = new HttpPost("your url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient hc = new DefaultHttpClient();
HttpResponse response = hc.execute(postMethod);
nameValuePairs用于在url中添加参数。
答案 1 :(得分:0)
这里ya go(适用于分页)
public void Execute(final Request request,final ClientCallBackHandler CallBack)
{
new Thread(new Runnable(){
@Override
public void run() {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
DefaultHttpClient httpClient = new DefaultHttpClient(myParams);
//dunno if this is correct...
if(CurrentConnection.SvUsr.length() > 0)
{
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(CurrentConnection.SvUrl, 80, null, "Digest"),
new UsernamePasswordCredentials(CurrentConnection.SvUsr, CurrentConnection.SvPw));
}
HttpContext localContext = new BasicHttpContext();
if(CurrentConnection != null)
{
HttpPost httpPost = new HttpPost(CurrentConnection.SvUrl);
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
String blah = SerializeRequest(request);
pairs.add(new BasicNameValuePair("request",blah));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
if(CallBack != null)
{
CallBack.SetStatus(ClientCallBackHandler.ENCODING_EXCEPTION);
}
return;
}
try {
if(CallBack != null)
CallBack.SetStatus(ClientCallBackHandler.HTTP_DID_START);
HttpResponse response = httpClient.execute(httpPost,localContext);
if(response != null)
{
if(CallBack != null)
{
CallBack.response = DeserializeResponse(EntityUtils.toString(response.getEntity()));
CallBack.SetStatus(ClientCallBackHandler.HTTP_DID_SUCCEED);
}
return;
}
else
{
if(CallBack != null)
{
CallBack.SetStatus(ClientCallBackHandler.HTTP_DID_ERROR);
}
return;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
if(CallBack != null)
{
CallBack.SetStatus(ClientCallBackHandler.CLIENT_PROTOCOL_EXCEPTION);
}
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
if(CallBack != null)
{
CallBack.SetStatus(ClientCallBackHandler.IO_EXCEPTION);
}
e.printStackTrace();
return;
}
}
}
}).start();
}
serializeRequest()方法将您的请求转换为服务可以理解的字符串...例如我的看起来像这样:
public String SerializeRequest(Request request) {
Gson gson = gsonb.create();
String JScript = gson.toJson(request);
Log.v(request.getClass().getName(),JScript);
return JScript;
}
DeserializeResponse()方法与此相反,所以在gson的情况下:
public Response DeserializeResponse(String Jscript)
{
Response response = null;
try {
JSONObject jsonObject = new JSONObject(Jscript);
Gson gson = gsonb.create();
response = gson.fromJson(jsonObject.toString(), Response.class);
if(response.ClassName.compareTo("ExceptionResponse")==0)
response = gson.fromJson(jsonObject.toString(), ExceptionResponse.class);
} catch (JSONException e) {
// TODO Auto-generated catch block
response = new ExceptionResponse();
((ExceptionResponse) response).Message = Jscript;
}
Log.v(response.getClass().getName(),Jscript);
return response;
}
答案 2 :(得分:0)
private void postData(final String param, final TextView tv) {
RequestQueue rq = Volley.newRequestQueue(this);
StringRequest postReq = new StringRequest(Request.Method.POST,
"https://your URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tv.setText(response); // We set the response data in the
// TextView
Toast.makeText(getApplicationContext(), "Response"+response,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error [" + error + "]");
Toast.makeText(getApplicationContext(), "No Internet!!!", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("your key", "value");
params.put("any callback methods", "value");
params.put("any types", "value");
return params;
}
};
rq.add(postReq);