package com.renren;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class HttpPost extends Activity{
URL url = null;
HttpURLConnection httpurlconnection = null;
@Override
public void onCreate(Bundle save)
{
super.onCreate(save);
try
{
url = new URL("https://graph.renren.com/oauth/token?");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
String param1="grant_type=authorization_code";
String param2="&code="+Util.code;
String param3= "&client_id=a4f9b6beaf874b59897c2ac29f3d81c4";
String param4= "&client_secret=2cd503937e7f49e7b7eac67c00544949";
String param5= "&redirect_uri=http://graph.renren.com/oauth/login_success.html";
httpurlconnection.getOutputStream().write((param1+param2+param3+param4+param5).getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
InputStream in=null;
in=httpurlconnection.getInputStream();
byte b[] = new byte[512];
int len = 0;
int temp=0;
int count=0;//所有读取的内容都使用temp接收
while((temp=in.read())!=-1){ //当没有读取完时,继续读取
b[len]=(byte)temp;
len++;
count++;
}
Log.i("code............... ", Integer.toString(code));
Log.i("acesstoken................",new String(b));
String content=new String(b).trim();
Log.i("content........",content);
String[] cn=content.split(",");
String access=cn[0].split(":")[1].replaceAll("\"","").trim();
String experise=cn[1].split(":")[1];
experise=experise.substring(0,experise.lastIndexOf("\n"));
Util.access_token=access;
Util.expires_in=experise;
//
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(httpurlconnection!=null)
httpurlconnection.disconnect();
}
getSessionKey();
}
public void getSessionKey()
{
try{
url = new URL("https://graph.renren.com/renren_api/session_key?");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
String param1="oauth_token="+Util.access_token;
httpurlconnection.getOutputStream().write(param1.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
InputStream in=null;
in=httpurlconnection.getInputStream();
byte b[] = new byte[1024];
int len = 0;
int temp=0;
int count=0;//所有读取的内容都使用temp接收
while((temp=in.read())!=-1){ //当没有读取完时,继续读取
b[len]=(byte)temp;
len++;
count++;
}
Log.i("code............... ", Integer.toString(code));
Log.i("acesstoken................",new String(b));
String content=new String(b).trim();
Log.i("content........",content);
String id=content.substring(content.lastIndexOf(":")+1,content.length()-2);
id=id.trim();
Util.userid=id;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(httpurlconnection!=null)
httpurlconnection.disconnect();
Intent intent=new Intent(this,MainAcitivty.class);
startActivity(intent);
}
}
}
- 1
- 2
前往页