【问题标题】:How to use httpget in my app如何在我的应用程序中使用 httpget
【发布时间】:2013-07-30 02:12:30
【问题描述】:

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

现在我已经完成了我的应用程序。我想将此消息发送到 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=网站&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%u92B817%u7684u% %u5C0E%u8005%20-%20MIGO%20CORP%20%u529F%u5178%u96C6%u5718%20&1375088741345

我需要用户的许可 INTERNET 吗? 以及如何实现?

【问题讨论】:

    标签: android asp.net http url


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      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,例如

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多