Help: HTTP Request with VP


#1

Hey guys,
I am currently struggling with an HTTP Request.
We’ve got a platform with all Objects either classes/tables etc. and I’m trying to receive those informations (which class/stereotype) so i can simply model them with my plugin instead of doing those manually.

However I’ve written an http request which does work in eclipse, but if i put it into my plugin it does not work.

to simplify i just tried to send a request to a website, than put the info into an array an print out just a single String but VP stops the moment the http request starts.

Any ideas what’s wrong?

I’m working with VP 10.2
my code

public class ClassGeneratorActionController implements VPActionController{
public void performAction(VPAction action) {

	ViewManager viewManager = ApplicationManager.instance().getViewManager();
	HTTP_Request almost = new HTTP_Request();
	String[][] table = almost.sendRequest();
	viewManager.showMessage(table[1][1]);
}}

//HTTP_Request Class

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;


public class HTTP_Request {


public String[][] content;

public String[][] sendRequest(){
try {
	
String url = "https://api.myjson.com/bins/1dvbb9"; //TABLE

  URL obj = new URL(url);
  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
  BufferedReader in = new BufferedReader(
  		new InputStreamReader(con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
  while ((inputLine = in.readLine()) != null) {
  	response.append(inputLine);
  	
  }
  in.close();
   
 JSONObject myresponse = new JSONObject(response.toString());
 JSONObject d_object = new JSONObject(myresponse.getJSONObject("d").toString());
 JSONArray results = new JSONArray(d_object.getJSONArray("results").toString());
  
 content = new String[results.length()][2];
   	 
 for(int i = 0; i<results.length();i++) {
	 
	JSONObject stereo = results.getJSONObject(i);
		 content[i][0] = stereo.getString("RET_VALUE");
		 content[i][1] = stereo.getString("RET_NAME");
}


  } catch (Exception e) {
   e.printStackTrace();
 }

return content;	
 }
}

I’ve used the java-json.jar
Thank you


#2

just forgot to add the jar library…
sorry^^


#3

If VP stopped then it sounds like your HTTP request isn’t responding as it should. Do you get any error logs within the console?

You might need to specify some additional request properties such as a request method which is mentioned in this link : http://www.baeldung.com/java-http-request

We use the following libraries and we have no problems making requests and iterating over its response:

  • java.net.HttpURLConnection;
  • org.json.simple