Android Registration Login interface, mainly write about the use of things.
First of all, the background with JSP write, here the words used to return data is JSON, can be used in a very simple way
<%@ page language="java" contentType="text/json; charset=UTF-8" pageEncoding="UTF-8"import="java.sql.*,java.io.*,java.util.*" %>
This modifies the contenttype= "Text/json" on it.
For clients, it is primarily
- Jumps between the interfaces
- Requests to the server
- Parsing of JSON data
1) Jump between interfaces
With Intent,intent i=new Intent (mainactivity.this,resactivity.class);
StartActivity (i)
2) Android Network requests there are three major ways we use httpclinet here.
First, Android does not allow HTTP requests to be placed on the mainline approached (easy to block), so you must create a new secondary
Thread to process the network Request section. The client (Defaulthttpclient Class) is used first for the specific request
Executes the request. Then use the HttpPost as a Post object (Get method with HttpGet), and then the post parameter
Settings, urlencodedformentity entity=new urlencodedformentity (parameter, "Utf-8");
parameter is a list of arrays.
Private void Get(FinalString name,FinalString password) {NewThread (NewRunnable () { Public void Run() {BufferedReader reader=NULL; StringBuffer sb=NULL; String result=""; Defaulthttpclient client=New defaulthttpclient(); String uri="http://10.65.89.152/server/servers.jsp"; List parameter=NewArrayList (); Parameter.Add(New Basicnamevaluepair("username", name)); Parameter.Add(New Basicnamevaluepair("Password", password)); HttpPost request=New HttpPost(URI);//httpresponse Responses=client.execute (request); Try{urlencodedformentity entity=New urlencodedformentity(Parameter,"Utf-8"); Request.setentity(entity);//toast.maketext (mainactivity.this, "Success1 Connect", Toast.length_short). Show (); //request.setentity (entity);HttpResponse response=client.Execute(request);//toast.maketext (mainactivity.this, "Success2 Connect", Toast.length_short). Show (); if(Response.Getstatusline().Getstatuscode() ==httpstatus.SC_OK){//toast.maketext (mainactivity.this, "Success Connect", Toast.length_short). Show ();Reader=NewBufferedReader (NewInputStreamReader (response.getentity().getcontent())); sb=NewStringBuffer (); String line=""; String Nl=system.GetProperty("Line.separator"); while(Line=reader.ReadLine())!=NULL) {sb.Append(line); } }//toast.maketext (mainactivity.this, "Faile Connect", Toast.length_short). Show ();}Catch(Clientprotocolexception e) {//toast.maketext (mainactivity.this, "Error Connect", Toast.length_short). Show ();E.Printstacktrace(); }Catch(IOException e) {//toast.maketext (mainactivity.this, "Error2 Connect", Toast.length_short). Show ();E.Printstacktrace(); }Try{if(NULL!=reader) {reader.Close(); Reader=NULL; } }Catch(IOException e) {e.Printstacktrace(); }if(NULL!=SB) {RESULT=SB.toString(); } Message Message =New Message(); Message. What= Show_response; Message.obj= result; Handler.SendMessage(message); Message messages=New Message(); } }).Start(); }
3) for the processing of JSON objects. The
uses a jar package Gson.
private void Parsejsonwithjsonobject (String jsondata) {
try {
//JSON string Jsondata loaded into a JSON array, that is, Jsonarray
// Jsondata can be read from a file, or you can get
Jsonarray jsonarray = new Jsonarray (jsondata) from the server side;
String State=null;
String Name=null;
String Id=null;
for (int i = 0; i< jsonarray.length (); i++) {
//loop traversal, remove Jsonobject object
//Remove corresponding key value with Getint and GetString method
Jsonobject Jsonobject = Jsonarray.getjsonobject (i);
//int stu_no = Jsonobject.getint ("Stu_no");
state=jsonObject.getString("code"); name=jsonObject.getString("name"); id=jsonObject.getString("id"); Log.d("MainActivity","code:"+state); } if(state.equals("success")){ Intent ii=new Intent(MainActivity.this,SecActivity.class); ii.putExtra("USERNAME",name); ii.putExtra("id", id); startActivity(ii); } } catch (Exception e) { e.printStackTrace(); }}
4) The main thread communicates with the sub-threads
public static final int show_response = 0;
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; //textviews.setText(response); parseJSONWithJSONObject(response); break; default: break; } } };
Android Registration Login Interface