REST-API

Diskussion i 'Frågor, support och diskussion' startad av blueWater, 7 jan 2012.

  1. blueWater

    blueWater Baby Droid Medlem

    Blev medlem:
    24 dec 2009
    Inlägg:
    29
    Mottagna gillanden:
    0

    MINA ENHETER

    Hej!

    Jag är en android utvecklare, och vill ge mig på att skriva kod till att logga in och använda tjänsterna på en CAS-server. Är det någon som vet om det finns java rest-api , som öppen källkod som kan användas till android utveckling?
    Finns det någon som kan förklara i stora drag vad som är viktigt att tänka på vid utveckling mot en CAS- server, och finns det något man ska tänka på vid urval av android-java rest-api? nyttiga länkar?

    Tack på förhand!
     
  2. Hemoroid

    Hemoroid Baby Droid Medlem

    Blev medlem:
    7 apr 2010
    Inlägg:
    26
    Mottagna gillanden:
    5

    MINA ENHETER

    Jag använder följande klass för rest-anrop (kommer tyvärr inte ihåg var jag hittade den)

    Kod:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    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.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.util.Log;
    
    public class RestClient {
    
    	public static String convertStreamToString(InputStream is) {
    		/*
    		 * To convert the InputStream to String we use the BufferedReader.readLine()
    		 * method. We iterate until the BufferedReader return null which means
    		 * there's no more data to read. Each line will appended to a StringBuilder
    		 * and returned as String.
    		 */
    		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    		StringBuilder sb = new StringBuilder();
    
    		String line = null;
    		try {
    			while ((line = reader.readLine()) != null) {
    				sb.append(line + "\n");
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				is.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    
    		return sb.toString();
    	}
    
    
    	/* This is a test function which will connects to a given
    	 * rest service and prints it's response to Android Log with
    	 * labels "Praeda".
    	 */
    	public static void connect(String url)
    	{
    
    		HttpClient httpclient = new DefaultHttpClient();
    
    
    		// Prepare a request object
    		HttpGet httpget = new HttpGet(url); 
    
    		// Execute the request
    		HttpResponse response;
    		try {
    			response = httpclient.execute(httpget);
    			// Examine the response status
    			Log.i("Praeda",response.getStatusLine().toString());
    
    			// Get hold of the response entity
    			HttpEntity entity = response.getEntity();
    			// If the response does not enclose an entity, there is no need
    			// to worry about connection release
    
    			if (entity != null) {
    
    				// A Simple JSON Response Read
    				InputStream instream = entity.getContent();
    				String result= convertStreamToString(instream);
    				Log.i("Praeda",result);
    
    				// A Simple JSONObject Creation
    				JSONObject json=new JSONObject(result);
    				Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
    
    				// A Simple JSONObject Parsing
    				JSONArray nameArray=json.names();
    				JSONArray valArray=json.toJSONArray(nameArray);
    				for(int i=0;i<valArray.length();i++)
    				{
    					Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
    							+"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
    				}
    
    				// A Simple JSONObject Value Pushing
    				json.put("sample key", "sample value");
    				Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
    
    				// Closing the input stream will trigger connection release
    				instream.close();
    			}
    
    
    		} catch (ClientProtocolException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (JSONException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    Den anropas sen typ så här

    Kod:
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 0);
    HttpResponse response = null;
    
    try {
    	HttpPost post = new HttpPost(url);
    	response = client.execute(post);
    } catch (Exception e) {
    	Logger.log(e.toString());
    }
    
    if (response != null) {
    	InputStream in;
    
    	try {
    		in = response.getEntity().getContent();
    		String result = RestClient.convertStreamToString(in);
    	} catch (IllegalStateException e) {
    		Logger.log(e.toString());
    	} catch (IOException e) {
    		Logger.log(e.toString());
    	}
    }
    
    Strängen result kommer då innehålla datan som hämtades hem
     
    blueWater gillar detta.
  3. blueWater

    blueWater Baby Droid Medlem

    Blev medlem:
    24 dec 2009
    Inlägg:
    29
    Mottagna gillanden:
    0

    MINA ENHETER

    hej hemoroid!

    tack-det hjälpte en hel del, koden är klar och tydlig och inte så svår heller :-). ska genast testa och implementera den i min kod, för att se vad jag får för resultat. :P