Класс контроллера
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package sample; import com.apivk.APIvk; import javafx.event.ActionEvent; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import java.io.IOException; public class Controller { public WebView view1; private String client_id = "********"; private String scope = "messages"; private String redirect_uri = "http://oauth.vk.com/blank.html"; private String display = "popup"; private String response_type = "token"; private String email = "********"; private String pass = "********"; public static String access_token; private String client_secret = "*********"; WebEngine webEngine; public void SetConnect(ActionEvent actionEvent) throws IOException { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost("http://oauth.vk.com/authorize?" + "client_id="+client_id+ "&scope="+scope+ "&redirect_uri="+redirect_uri+ "&display="+display+ "&response_type="+response_type + "&v=5.52"); HttpResponse response; response = httpClient.execute(post); post.abort(); StatusLine status = response.getStatusLine(); System.out.println(status); webEngine = view1.getEngine(); webEngine.load(post.getURI().toString()); } public void GetToken(ActionEvent actionEvent) throws IOException { String HeaderLocation = webEngine.getLocation(); access_token = HeaderLocation.split("#")[1].split("&")[0].split("=")[1].toString(); // new Msg("Connection OK, access_token = " + access_token); APIvk.GetMessage(); } } |
Класс APIvk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.apivk; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import sample.Controller; import sample.com.messages.Msg; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class APIvk { public static void GetMessage () throws IOException { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost( "https://api.vk.com/method/" + "messages.get" + "?out=0" + "&access_token=" + Controller.access_token); HttpResponse response = httpClient.execute(post); post.abort(); String line = ""; URL url = new URL(post.getURI().toString()); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); line = reader.readLine(); reader.close(); new Msg(line); } } |