How to use httpget in my app

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=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

Do I need user’s permission INTERNET?
And how to implement this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x