Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;
import android.util.Log;

public class Main {
    private static String TAG = "XJTUAuthUtils";

    public static boolean getkeepaliveinfo() {
        String baseUrl = "http://10.6.8.2/cgi-bin/keeplive?";

        try {
            HttpGet getMethod = new HttpGet(baseUrl);
            getMethod.addHeader("Accept", "*/*");
            //getMethod.addHeader("Accept-Language", "zh-cn");
            //getMethod.addHeader("Referer", "http://202.117.2.41/index.html");
            //getMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");
            //getMethod.addHeader("Accept-Encoding", "gzip, deflate");
            //getMethod.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
            getMethod.addHeader("Host", "10.6.8.2");
            //getMethod.addHeader("DNT", "1");

            HttpClient httpClient = new DefaultHttpClient();

            HttpResponse response = httpClient.execute(getMethod);
            Log.i(TAG, "Sending message.....");
            HttpEntity httpEntity = response.getEntity();
            if (response.getStatusLine().getStatusCode() == 200) {
                String message = EntityUtils.toString(httpEntity);
                if (httpEntity == null || message.compareTo("error") == 0) {
                    Log.i(TAG, "Get keepalive info failed!!!message=" + message);
                    return false;
                } else {
                    Log.i(TAG, "Get keepalive info succeed!!!message=" + message);
                    return true;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "Get keepalive info failed!!!");
        return false;
    }
}