如何使用HTTPGET在我的应用程序 [英] How to use httpget in my app
问题描述
我很对不起我的英文不好,差的Java编程。请告诉我细节。感谢这么多的帮助。
现在我已经完成了我的应用程序。我wnat发送此邮件为 mywebsite .COM。
http://mywebsite.com/web_service/counter/CSTrack2.aspxcli=focusgroup&cam=androidtest&1=W_S_UID&2=W_S_SC&3=W_S&5=W_S_DATE&7=www.mywebsite.com&8=www.mywebsite.com%2Ftc%2F&9=&10=W_S_IP&11=www.mywebsite.com&12=www.mywebsite.com%2F&13=&14=website&15=W_S&16=W_S&17=W_S&19=windows&20=Win%207(x64)&21=1&22=9.0&23=1366x768&24=1&25=1&6=CRM%20%26%20%u6578%u4F4D%u884C%u92B7%u7684%u9818%u5C0E%u8005%20-%20MIGO%20CORP%20%u529F%u5178%u96C6%u5718%20&1375088741345
我是否需要用户的权限上网? 而如何实现这一点?
公共类AsyncTaskExample扩展的AsyncTask<字符串,太虚,布尔> {
私人ProgressDialog progressDialog;
私人活动的活动;
公共AsyncTaskExample(活动活动,ProgressDialog进度){
活动=活动;
progressDialog =进展情况;
}
@覆盖
在preExecute保护无效(){
progressDialog.show();
}
@覆盖
保护布尔doInBackground(字符串... PARAMS){
// params为包含值的字符串这是通过在调用此AsyncTaskExample阵列
INT响应code;
字符串responseString;
名单< BasicNameValuePair> namevaluepairs中= NULL;
的HttpParams httpParameters =新BasicHttpParams();
//毫秒设置超时,直到建立了连接。
INT timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO_TIMEOUT)
//毫秒这是超时等待数据。
INT timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
HttpClient的HttpClient的=新DefaultHttpClient(httpParameters);
HttpPost httpPost =新HttpPost(YOUR_URL);
HTT presponse响应;
namevaluepairs中=新的ArrayList< BasicNameValuePair>();
nameValuePairs.add(新BasicNameValuePair(用户,varTwo));
nameValuePairs.add(新BasicNameValuePair(密码,varThree));
httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
响应= httpclient.execute(httpPost);
。响应code = response.getStatusLine()的getStatus code();
responseString = EntityUtils.toString(response.getEntity());
返回isSuccess;
}
@覆盖
保护无效onPostExecute(布尔isSuccess){
尝试 {
progressDialog.dismiss();
}赶上(例外五){
Log.e(TAG,错误而关闭对话框);
}
}
somwhere致电AsyncTaskExample在你code,像
AsyncTaskExample loginTask =新AsyncTaskExample(这一点,progressDialog);
task.execute(用户名,密码);
//这里用户名,密码都应该PAASS你的web服务的参数,可以将得到doInBackGroung随参数变量PARAMS [0],则params [1]
你还应该在清单文件上网权限
I'm so sorry for my poor English and poor programming in java. Please teach me the detail. Thank so much for help.
Now I've finished my app. And I wnat to send this message to mywebsite.com.
http://mywebsite.com/web_service/counter/CSTrack2.aspxcli=focusgroup&cam=androidtest&1=W_S_UID&2=W_S_SC&3=W_S&5=W_S_DATE&7=www.mywebsite.com&8=www.mywebsite.com%2Ftc%2F&9=&10=W_S_IP&11=www.mywebsite.com&12=www.mywebsite.com%2F&13=&14=website&15=W_S&16=W_S&17=W_S&19=windows&20=Win%207(x64)&21=1&22=9.0&23=1366x768&24=1&25=1&6=CRM%20%26%20%u6578%u4F4D%u884C%u92B7%u7684%u9818%u5C0E%u8005%20-%20MIGO%20CORP%20%u529F%u5178%u96C6%u5718%20&1375088741345
Do I need user's permission INTERNET? And how to implement this?
public class AsyncTaskExample extends AsyncTask<String, Void, Boolean>{
private ProgressDialog progressDialog;
private Activity activity;
public AsyncTaskExample(Activity activity, ProgressDialog progress) {
activity = activity;
progressDialog = progress;
}
@Override
protected void onPreExecute(){
progressDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
//params is array of strings containing values which are passing while calling this AsyncTaskExample
int responseCode;
String responseString;
List<BasicNameValuePair> nameValuePairs = null;
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost("YOUR_URL");
HttpResponse response;
nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user", varTwo ));
nameValuePairs.add(new BasicNameValuePair("password", varThree ));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httpPost);
responseCode = response.getStatusLine().getStatusCode();
responseString = EntityUtils.toString(response.getEntity());
return isSuccess;
}
@Override
protected void onPostExecute(Boolean isSuccess){
try {
progressDialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "Error while closing dialog");
}
}
Call the AsyncTaskExample somwhere in you code, like
AsyncTaskExample loginTask = new AsyncTaskExample (this, progressDialog);
task.execute(userName, password);
// here userName, password are the params which should paass to your webservice, it will get in doInBackGroung by params variable as params[0], params[1]
you should also have internet permission in manifest file
这篇关于如何使用HTTPGET在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!