如何在我的应用程序中使用httpget

时间:2013-07-30 04:37:50

标签: android asp.net http url

对于我糟糕的英语和糟糕的java编程,我很抱歉。请教我细节。非常感谢您的帮助。

现在我已经完成了我的应用。我希望将此消息发送到 mywebsite .com。

的http:// mywebsite 的.com /使用web_service /计数器/ CSTrack2.aspxcli =焦点小组&安培;凸轮= 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 =网站&安培; 15 = W_S&安培; 16 = W_S&安培; 17 = W_S&安培; 19 =窗户&安培; 20 =赢% 207(64)及21 = 1&安培; 22 = 9.0&安培; 23 = 1366×768&安培; 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

我是否需要用户的许可INTERNET? 以及如何实现这个?

2 个答案:

答案 0 :(得分:0)

对于android中的任何网络通信,你必须在Manifest文件中声明INTERNET权限,而且所有网络通信都应该在一个单独的线程上完成(使用Async Task),否则会引发运行时异常。

答案 1 :(得分:0)

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");
        }

    }

在代码中的某个位置调用异步任务示例,例如

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