2011-08-18 5 views
0

안드로이드에서 내 코드에서 assembla apis를 호출하려고합니다. 아래 코드를 사용하여 assembla에 연결할 수 있습니다. HTMl 형식으로 출력됩니다. 이 데이터를 사용하는 방법을 이해할 수 없습니다.안드로이드 프로그램에서 Assembla REST API를 호출하는 방법

코드 조각 :

HttpURLConnection의 CONN = NULL; 시도 {

 String authentication = "username:password"; 
     String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP); 
     URL url = new URL("https://www.assembla.com/spaces/my_spaces"); 
     //URL url = new URL("https://www.assembla.com/"); 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Authorization", "Basic " + encoding); 
     conn.setDoOutput(true); 
     conn.connect(); 

     System.out.println(conn.getResponseCode()); 
     System.out.println(conn.getResponseMessage()); 

     InputStreamReader isr = 
      new InputStreamReader(conn.getInputStream()); 
     BufferedReader br = new BufferedReader(isr); 

     String inputLine; 

     while ((inputLine = br.readLine()) != null) 
      System.out.println(inputLine); 

     br.close(); 

나는 내가 나머지 API를 호출 사용하고 접근이 정확한지 여부를 그렇게 아무 생각 안드로이드 없음 아주 새로운 오전. 상담하십시오.

감사합니다.

답변

0

이것은 내가 주로 내 API 요청을하는 방법입니다.

 HttpClient c = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("URL"); 

     post.addHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8"); 
     List<NameValuePair> httpparams = new ArrayList<NameValuePair>(); 

     httpparams.add(new BasicNameValuePair("password", password)); 

     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(httpparams); 
     post.setEntity(entity); 

     HttpResponse resp = c.execute(post); 


     InputStream i = resp.getEntity().getContent(); 
     InputStreamReader ir = new InputStreamReader(i); 
     BufferedReader br = new BufferedReader(ir); 

     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = br.readLine()) != null) { 
      sb.append(line); 
     } 

     ir.close(); 
     br.close(); 

     JSONObject user = new JSONObject(sb.toString()); 

원하는 경우 매개 변수를 추가하거나 제거 할 수 있습니다. 는 그리고 난 당신이 JSON 개체를받을 asume (sb.toString())

편집 : 그것은 POST가있어이 경우, 당신은 쉽게 GET을 변경할 수 있습니다 (HttpGet)

관련 문제