Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Code cleanup and additional checks #30

Merged
merged 2 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/me/checkium/vhackapi/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ public static String readJson(Reader rd) throws IOException {
public static JSONObject JSONRequest(String format, String data, String php){
JSONObject json = null;
String jsonText = StringRequest(format, data, php);
if (jsonText.length() == 1) {
if("".equals(jsonText))
{
throw new RuntimeException("Old API URL");
}
else if("8".equals(jsonText))
{
throw new RuntimeException("Wrong Password/User");
}
else if (jsonText.length() == 1) {
return null;
}
json = new JSONObject(jsonText);
return json;
}

//it'll just do the request without any checks
public static String StringRequest(String format, String data, String php)
{
System.setProperty("http.agent", "Chrome");
Expand Down
21 changes: 2 additions & 19 deletions src/main/java/me/checkium/vhackapi/vHackAPI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package me.checkium.vhackapi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import org.json.JSONObject;
Expand Down Expand Up @@ -43,19 +37,8 @@ public String getStats(Stats stat) {
} catch (InterruptedException e1) {
e1.printStackTrace();
}
JSONObject json = null;
InputStream is;
try {
is = new URL(Utils.generateURL("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_update.php")).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = Utils.readJson(rd);
json = new JSONObject(jsonText);
if (json.getString(stat.toString()) == null) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
JSONObject json = Utils.JSONRequest("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_update.php");

return json.getString(stat.toString());
}

Expand Down