我正在開發一個涉及解析的android應用程序。我想在android中將參數傳遞給url。我應該如何繼續?在android中傳遞參數到url
2
A
回答
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中添加參數。
0
這裏亞去(可與尋呼大)
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()梅索德轉換的youre請求轉換成字符串的youre服務可以理解...例如我的是這樣看:
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;
}
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);
相關問題
- 1. 傳遞參數到一個URL AsyncTask android
- 2. 傳遞URL參數在JavaScript
- 3. Android:通過url傳遞參數
- 4. 參數沒有被傳遞到Android中的URL
- 5. Laravel在URL中傳遞參數
- 6. 在Silverlight URL中傳遞參數
- 7. 在OData中傳遞參數WebApi Url
- 8. 在formtastic url中傳遞可選參數
- 9. 在CodeIgniter中通過URL傳遞參數
- 10. 如何在jQuery中傳遞url參數
- 11. jQuery Mobile在URL中傳遞參數
- 12. 在URL中傳遞多個參數
- 13. 如何在URL中傳遞POST參數?
- 14. Ruby on Rails在URL中傳遞參數
- 15. 在參數MVC中傳遞URL 4
- 16. 在JSF中通過URL傳遞參數
- 17. 在URL搜索中傳遞參數
- 18. 在URL中傳遞額外的參數
- 19. 在WordPress URL中傳遞查詢參數
- 20. 從android中傳遞byte []參數到soap.Net
- 21. 骨幹傳遞參數到模型url
- 22. 傳遞參數到crm 2011 url
- 23. 如何將url參數傳遞到iframe?
- 24. 如何傳遞url參數到templateUrl
- 25. 錯誤的參數傳遞到URL
- 26. 將jquery的URL參數傳遞到php
- 27. 將URL參數傳遞到iFrame源
- 28. 從URL傳遞參數到mailto命令
- 29. 從url傳遞參數到cakephp
- 30. url參數傳遞機制
試試這個[link](http://www.androidpeople.com/xml-parsing) – 2011-03-16 06:43:06