I am develop开发者_开发技巧ing an android application which involves parsing.I want to pass parameters to url in android. How should I proceed?
Please Try This.
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 are used to add parameter in url .
here ya go (works great with paging)
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();
}
the SerializeRequest() methode converts youre Request into a String youre service can understand ... for example mine is looking like this:
public String SerializeRequest(Request request) {
Gson gson = gsonb.create();
String JScript = gson.toJson(request);
Log.v(request.getClass().getName(),JScript);
return JScript;
}
the DeserializeResponse() methode does pretty much the opposite of this, so in case of 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;
}
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);
精彩评论