How to use httpget in my app

2019-01-29 13:18发布

问题:

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?

回答1:

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


回答2:

For any network communication in android you will have to declare INTERNET permission in Manifest file moreover all network communication should be done on a separate thread(use Async Task) otherwise it will throw a runtime exception.