답변

1

1º 매니페스트에서 인터넷 사용 권한을 부여하십시오.

2 º 사용하십시오 AsyncTask를 내부에이 기능이 :

private final String GOOGLE_URL = "https://www.googleapis.com/urlshortener/v1/url"; 

public static String getShortUrl(String _url){ 
    HttpParams httpParameters = new BasicHttpParams(); 
    int timeoutConnection = 5000; 
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
    int timeoutSocket = 10000; 
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

    HttpClient hc = new DefaultHttpClient(httpParameters); 

    HttpPost request = new HttpPost(GOOGLE_URL); 
    request.setHeader("Content-type", "application/json"); 
    request.setHeader("Accept", "application/json"); 

    JSONObject obj = new JSONObject(); 
    obj.put("longUrl", _url); 
    request.setEntity(new StringEntity(obj.toString(), "UTF-8")); 

    HttpResponse response = hc.execute(request); 

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
    { 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    response.getEntity().writeTo(out); 
    out.close();    
    return out.toString(); 
    } 
    else { 
    return null; 
    } 
    } 
    catch (Exception e) { 
    e.printStackTrace();   
    } 
    return null; 
} 
+0

이 구글 바로 플레이 서비스 포함되지 않습니다? –

+0

그래, Google Play 서비스가 필요하지 않습니다. – extmkv

관련 문제