2013-03-28 109 views
0
package com.example.intracollegeapp;// package name 

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.ObjectInput; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutput; 
import java.io.ObjectOutputStream; 
import java.net.URL; 
import java.net.URLConnection; 
import java.security.MessageDigest; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 


import LibPack.UserInfoLib; 
import android.R.string; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.support.v4.app.NavUtils; 

public class LoginForm extends Activity { 
    Button login; 
    TextView username; 
    TextView password; 
    UserInfoLib ui; 
    long msgLength; 
    long bitLength; 
    char msg[]; 
    long requiredBits; 
    long requiredBytes; 
    int toPad[]; 
    private static final String NAMESPACE = "link to server package on which  webservices are stored"; 
    private static final String URL = "link to wsdl file stored on server"; 
    private static final String SOAP_ACTION = "IntraCollegeWS"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login_form); 
     login=(Button)findViewById(R.id.butLogin); 
     username=(TextView)findViewById(R.id.editText1); 
     password=(TextView)findViewById(R.id.editText2); 
     login.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       ui= new UserInfoLib(); 
       ui.userId=username.getText().toString(); 
       ui.password=getSHA1(password.getText().toString()); 

       ui=(UserInfoLib)callService(objectToString(ui), "UserLogin", "userInfo"); 
       //ui=(UserInfoLib)stringToObject(temp); 

       if(ui.firstName.equals("")){ 
        Toast.makeText(v.getContext(), "Please Verify User Name Or Password", Toast.LENGTH_LONG).show(); 
        finish(); 

       }else{ 
        Toast.makeText(v.getContext(), "Login Successfull", Toast.LENGTH_LONG).show(); 
        System.out.println("NAME :"+ui.firstName); 
        Intent i = new Intent(v.getContext(), MainForm.class); 
        i.putExtra("uid", ui); 
        startActivity(i); 
        finish(); 
       } 
      } 
     }); 

    } 

    public long leftRotateBy(long l, int times) { 
     return ((l << times) & 0xFFFFFFFFl) | ((l & 0xFFFFFFFFl) >> (32 - times)); 
    } 

    public int getByteAt(int at) { 
     if (at < msgLength) { 
      return (msg[at]); 
     } else { 
      at = at - (int) msgLength; 
      return toPad[at]; 
     } 
    } 

    public void padBits(String pass) { 
     System.out.println("\n\n\n\n"); 

     msg = pass.toCharArray(); 
     msgLength = msg.length; 
     bitLength = msgLength * 8; 

     System.out.println("Msg Bit Length: " + bitLength); 
     System.out.println("MSg Byte Length: " + msgLength); 
     System.out.println("Required Minimum Bits: " + (bitLength + 65)); 

     long remainder = (bitLength + 65) % 512; 
     System.out.println("Mod (Bits): " + remainder); 


     if (remainder == 0) { 
      requiredBits = 65; 
      System.out.println("No Padding Needed."); 
     } else { 
      requiredBits = (512 - remainder) + 65; 
      System.out.println(requiredBits + " Bits Padding Needed."); 
     } 

     requiredBytes = requiredBits/8; 
     toPad = new int[(int) requiredBytes]; 
     System.out.println("Required Bits: " + requiredBits); 
     System.out.println("Required Bytes: " + requiredBytes); 

     // manually append 1 to start of pad bits... 
     toPad[0] = 0x80; 
     for (int i = 1; i < requiredBytes - 8; i++) { 
      toPad[i] = 0; 
     } 

     long temp = bitLength; 
     for (int i = (int) (requiredBytes - 1); i >= (int) (requiredBytes - 8); i--) { 
      int t = (int) (temp & 0xff); 
      temp = temp >> 8; 
     toPad[i] = t; 
     } 

     System.out.println("TO PAD: "); 
     for (int i = 0; i < requiredBytes; i++) { 
      System.out.print(Integer.toHexString(toPad[i]) + " "); 
     } 
     System.out.println(); 
    } 

    public String getSHA1(String pass) { 
     int kconst[] = new int[]{ 
       0x5A827999, 
       0x6ED9EBA1, 
       0x8F1BBCDC, 
       0xCA62C1D6}; 

     long h0 = 0x67452301; 
     long h1 = 0xEFCDAB89; 
     long h2 = 0x98BADCFE; 
     long h3 = 0x10325476; 
     long h4 = 0xC3D2E1F0; 
     long a, b, c, d, e; 
     padBits(pass); 

     long totalLength = msgLength + requiredBytes; 
     System.out.println("TOTAL LENGTH: " + totalLength); 
     System.out.println("BLOCKS: " + (totalLength/8)); 

     long w[] = new long[80]; 
     for (int i = 0; i < (int) totalLength; i += 64) { 
      for (int j = i, kk = 0; j < (i + 64); j += 4, kk++) { 
       w[kk] = 0xffffffffl & ((getByteAt(j) << 24) | (getByteAt(j + 1) << 16) | (getByteAt(j + 2) << 8) | (getByteAt(j + 3))); 
       //System.out.println("W[" + kk + "]: " + Long.toHexString(w[kk])); 
      } 

      for (int kk = 16; kk < 80; kk++) { 
       w[kk] = (w[kk - 3]^w[kk - 8]^w[kk - 14]^w[kk - 16]); 
       w[kk] = leftRotateBy(w[kk], 1); 
       //System.out.println("W[" + kk + "]: " + Long.toHexString(w[kk])); 
      } 
      a = h0; 
      b = h1; 
      c = h2; 
      d = h3; 
      e = h4; 

      long temp = 0; 
      for (int t = 0; t < 20; t++) { 
       temp = leftRotateBy(a, 5) + ((b & c) | ((~b) & d)) + e + w[t] + kconst[0]; 
       temp &= 0xFFFFFFFFl; 
       e = d; 
       d = c; 
       c = leftRotateBy(b, 30); 
       b = a; 
       a = temp; 
      } 

      for (int t = 20; t < 40; t++) { 
       temp = leftRotateBy(a, 5) + (b^c^d) + e + w[t] + kconst[1]; 
       temp &= 0xFFFFFFFFl; 
       e = d; 
       d = c; 
       c = leftRotateBy(b, 30); 
       b = a; 
       a = temp; 
      } 

      for (int t = 40; t < 60; t++) { 
       temp = leftRotateBy(a, 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] + kconst[2]; 
       temp &= 0xFFFFFFFFl; 
       e = d; 
       d = c; 
       c = leftRotateBy(b, 30); 
       b = a; 
       a = temp; 
      } 

      for (int t = 60; t < 80; t++) { 
       temp = leftRotateBy(a, 5) + (b^c^d) + e + w[t] + kconst[3]; 
       temp &= 0xFFFFFFFFl; 
       e = d; 
       d = c; 
       c = leftRotateBy(b, 30); 
       b = a; 
       a = temp; 
      } 

      h0 = (h0 + a) & 0xFFFFFFFFl; 
      h1 = (h1 + b) & 0xFFFFFFFFl; 
      h2 = (h2 + c) & 0xFFFFFFFFl; 
      h3 = (h3 + d) & 0xFFFFFFFFl; 
      h4 = (h4 + e) & 0xFFFFFFFFl; 
     } 
     return Long.toHexString(h0) + Long.toHexString(h1) + Long.toHexString(h2) + Long.toHexString(h3) + Long.toHexString(h4); 


    } 

    Object callService(String INPUT_DATA, String METHOD_NAME, String PARAMETER_NAME){ 

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     PropertyInfo pi = new PropertyInfo(); 
     pi.setName(PARAMETER_NAME); 
     pi.setValue(INPUT_DATA); 
     pi.setType(String.class); 

     request.addProperty(pi); 

     envelope.setOutputSoapObject(request); 

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      SoapObject resultsRequestSOAP = (SoapObject)envelope.bodyIn; 

      String resp = resultsRequestSOAP.getPrimitivePropertyAsString("return"); 

      return stringToObject(resp); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    Object stringToObject(String inp){ 
     byte b[] = Base64.decode(inp); 
     Object ret = null; 
     try { 
      ByteArrayInputStream bis = new ByteArrayInputStream(b); 
      ObjectInput in = new ObjectInputStream(bis); 
      ret = (Object) in.readObject(); 
      bis.close(); 
      in.close(); 
     } catch(Exception e) { 
      System.out.println("NOT DE-SERIALIZABLE: " + e); 
     } 
     return ret; 
    } 

    String objectToString(Object obj){ 
     byte[] b = null; 
     try { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutput out = new ObjectOutputStream(bos); 
      out.writeObject(obj); 
      b = bos.toByteArray(); 
     } catch(Exception e) { 
      System.out.println("NOT SERIALIZABLE: " + e); 
     }   
     return Base64.encode(b); 
    } 





} 

/*我开发了一个Android应用程序连接到服务器的登录目的。为了连接,我使用了ksoap2库。 Intracollege webservice存储在服务器上。当连接到使用WiFi的服务器时,应用程序工作正常。如果它没有连接到WiFi,它会显示消息“应用程序崩溃”,然后应用程序停止工作。 我只想显示一个简单的消息“应用程序未连接到服务器”如果使用WiFi未连接到服务器。*/我的Android应用程序崩溃,如果它没有连接到WiFi

+1

检查登录按钮clicklistener中的Internet连接。 – rajeshwaran 2013-03-28 06:20:54

+1

参考这个http://stackoverflow.com/questions/2973290/check-wifi-and-gps-isconnected-or-not-in-android http://stackoverflow.com/questions/5840760/how-can-i-签是否器件用罐存取互联网通有源WiFi的连接 – rajeshwaran 2013-03-28 06:22:24

回答

0

试试这个........

public boolean isNet() 
{ 
boolean status=false; 
String line; 
try 
{ 
    URL url = new URL("http://www.google.com"); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
    while((line = reader.readLine()) != null) 
    { 
    } 
    status=true; 
} 
catch (IOException ex) 
{ 
    System.out.println("ex in isNet : "+ex.toString()); 
    if(ex.toString().equals("java.net.UnknownHostException: www.google.com")) 
     status=false; 
} 
catch(Exception e) 
{ 

} 
return status; 
} 


if(status==true) 
     { 
     //Do your operation 
     } 
     else 
      show("No Internet Connection."); 

当状态为真时,请执行登录过程。否则显示消息“应用程序未连接到服务器”。

0

要检查互联网连接尝试了这一点


private boolean haveNetworkConnection() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
} 
0

检查这个代码,这将帮助你:

初始化在要检查网络可用性类。

OtherUtils otherUtils = new OtherUtils(); 
if (!otherUtils.isNetworkAvailable(getApplicationContext())) { 
      Toast.makeText(getApplicationContext(), "No Network Available", Toast.LENGTH_LONG).show(); 
      return; 
     } 

添加以下类:

public class OtherUtils{ 
    Context context; 
    public boolean isNetworkAvailable(Context context) { 
       this.context = context; 

       ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
       if (connectivity == null) { 
       // boitealerte(this.getString(R.string.alertNoNetwork),"getSystemService rend null"); 
       } else { 
        NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
        if (info != null) { 
        for (int i = 0; i < info.length; i++) { 
         if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
          return true; 
         } 
        } 
        } 
       } 
       return false; 
      } 
    } 

这必将帮助你。

相关问题