直接上代碼java
LoginActivity.javaandroid
package LoginActivity.activity; import LoginActivity.bean.StringUtils; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.haierloginactivity.R; public class haierLoginActivity extends Activity { private Button btjButton; private EditText uname; private EditText password; private InputMethodManager in; public void onCreate (Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); in = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); btjButton = (Button)findViewById(R.id.button); uname = (EditText)findViewById(R.id.username); password = (EditText)findViewById(R.id.password); btjButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub in.hideSoftInputFromWindow(v.getWindowToken(), 0); String name = uname.getText().toString(); String pwd = password.getText().toString(); if(StringUtils.isEmpty(name)){ Toast.makeText(v.getContext(), R.string.login_uname_null, Toast.LENGTH_SHORT); return; } if(StringUtils.isEmpty(pwd)){ Toast.makeText(v.getContext(), R.string.login_pwd_null, Toast.LENGTH_SHORT); return; } login(name, pwd); } }); } private void login(final String name, final String pwd){ final Handler handler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1){ //提示登錄成功 Toast.makeText(haierLoginActivity.this, R.string.msg_login_success, Toast.LENGTH_SHORT).show(); finish(); }else if(msg.what == 0){ Log.i(name, msg.toString()); }else if(msg.what == -1){ Log.i("==============", msg.toString()); } } }; new Thread(){ public void run() { Message msg =new Message(); try { post po = new post(); String infoo = po.logina(name, pwd); if(infoo != null){ msg.what = 1;//成功 msg.obj = infoo; }else{ msg.what = 0;//失敗 msg.obj = "2"; } } catch (Exception e) { e.printStackTrace(); msg.what = -1; msg.obj = e; } handler.sendMessage(msg); } }.start(); } }
post.javaapache
package LoginActivity.activity; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.client.HttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class post { public String logina(String name, String pwd)throws ParseException, IOException, JSONException{ HttpClient httpClient = new DefaultHttpClient(); String strurl = "//此處改爲本身的url"; HttpPost request = new HttpPost(strurl); request.addHeader("Accept","application/json"); request.addHeader("Content-Type","application/json");//還能夠自定義增長header JSONObject param = new JSONObject();//定義json對象 param.put("sequenceId", "87620056570355357690"); param.put("accType", "0"); param.put("loginId", name); param.put("password", pwd); //param.put("thirdpartyAppId", ""); //param.put("thirdpartyAccessToken", ""); param.put("loginType", "1"); Log.i("===========", param.toString()); StringEntity se = new StringEntity(param.toString()); request.setEntity(se);//發送數據 HttpResponse httpResponse = httpClient.execute(request);//得到相應 int code = httpResponse.getStatusLine().getStatusCode(); System.out.print(code); String result = EntityUtils.toString(httpResponse.getEntity()); JSONObject result1 = new JSONObject(result); String info = (String) result1.get("retInfo"); Log.i("=============", info); return info; } }
要記得給設備訪問網絡權限json
<uses-permission android:name="android.permission.INTERNET"></uses-permission>網絡