格式化JSON对象并通过齐射发送到服务器的正确方法

时间:2018-06-01 00:50:31

标签: java android json android-volley

我尝试使用谷歌的Safe Browsing API检查网页链接是否列入黑名单,只需要在JSON对象中发送请求:

POST https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY HTTP/1.1
Content-Type: application/json

JSON对象应该是:

{
 "client": {
   "clientId":      "mycompanyname",
   "clientVersion": "1.1"
 },
 "threatInfo": {
   "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
   "platformTypes":    ["WINDOWS"],
   "threatEntryTypes": ["URL"],
   "threatEntries": [
     {"url": "http://www.urltocheck1.org/"},
     {"url": "http://www.urltocheck2.org/"},
     {"url": "http://www.urltocheck3.com/"}
   ]
 }
}

但是我已经不知道我是否正确地格式化了JSON对象,或者我的代码是否正确,请参阅下文:

public class SB {
  private ArrayList<String> url; //URL's a analizar
  private String key; //API key
  private RequestQueue queue;
  private Context context;
  private ArrayList<SBthreat> AnalyzedUrl; //analisis final
  private boolean Interrupted = false;
  private boolean CallFail = false;

  public SB(Context context, ArrayList<String> url, String key){
      this.url = url;
      this.key = key;
      this.context = context;
      queue = Volley.newRequestQueue(context);

      AnalyzedUrl = new ArrayList<>();
  }

  public void Request(){
      final StringBuilder api = new StringBuilder("https://safebrowsing.googleapis.com/v4/threatMatches:find?key=");
      JSONObject requestBody = new JSONObject();

      //JSON body
      try {
          JSONObject client = new JSONObject();
          client.put("clientId", "NetworkSentinel");
          client.put("clientVersion", "1.2");

          JSONObject threatEntries = new JSONObject();

          for(int i = 0; i < this.url.size(); i++){
              threatEntries.put("url", this.url.get(i)); //-----> the url can be too many
          }

          JSONObject threatInfo = new JSONObject();
          threatInfo.put("threatTypes", "[\"MALWARE\", \"SOCIAL_ENGINEERING\"]");
          threatInfo.put("platformTypes", "[\"WINDOWS\"]");
          threatInfo.put("threatEntryTypes", "[\"URL\"]");
          threatInfo.put("threatEntries", threatEntries);

          JSONObject jsonBody = new JSONObject();
          jsonBody.put("client", client);
          jsonBody.put("threatInfo", threatInfo);

          requestBody.put("", jsonBody);

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

      api.append(key).append(" HTTP/1.1");

      Log.i("SB", api.toString().replace(key, "XXXXXXXXXXXXX"));

      RequestFuture<JSONObject> future = RequestFuture.newFuture();

      JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, api.toString(), requestBody, future, future){
          @Override
          public HashMap<String, String> getHeaders() {
              HashMap<String, String> params = new HashMap<>();
              params.put("Content-Type", "application/json");
              return params;
          }
      };

      queue.add(request);

      try {
          JSONObject response = future.get();
          if(response.length() != 0) {

              Log.i("SB", "-----------------------------------------------------------------------");
              Log.i("response", response.toString());

              Interrupted = false;
              CallFail = false;
          }
      } catch (InterruptedException e) {
          e.printStackTrace();
          Interrupted = true;
      } catch (ExecutionException e) {
          e.printStackTrace();
          CallFail = true;
      }
  }

  public ArrayList<SBthreat> GetAnalysis(){
      return AnalyzedUrl;
  }

  public boolean isInterrupted(){
      return Interrupted;
  }

  public boolean isCallFail(){
      return CallFail;
  }
}

代码threatInfo.put("threatTypes", "[\"MALWARE\", \"SOCIAL_ENGINEERING\"]");是否写得好?或者有更好的方法将数据放在方括号中?

当我运行我的代码时,总是得到错误com.android.volley.ClientError和连接错误,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该使用JSONArray,不要手动放置 <div *ngFor="let element of elements"> <div *ngFor="let data of element"> <button class="btn btn-primary" (click)="callModal(data)">Open Modal and pass this data to a modal</button> </div> </div> <app-form-modal></app-form-modal> [。请参考以下内容:

]