2016-12-24 5 views
0

Ich arbeite an einer Android App für einen Kunden, und ich rufe ihre API, um die Informationen für verschiedene Teile meiner App zu erhalten. Es gibt einen Aufruf, der zu SocketTimeoutException führt, wenn ich ein Timeout festlege, oder unendlich hänge, wenn ich es nicht mache; Es funktioniert jedoch einwandfrei auf dem Webclient (React), also kann es nicht der Server sein.Android HTTPUrlConnection SocketTimeoutException/Indefinite Hang?

Code:

package io.voluntu.voluntu; 

import android.os.AsyncTask; 
import android.os.Bundle; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Arrays; 

public class SendApproveHours extends AsyncTask<Bundle, Void, String>{ 
    private StringBuilder sb = new StringBuilder(); 
    private String result; 
    private ApproveHours approveHours; 

    public SendApproveHours(ApproveHours approveHours){ 
     this.approveHours = approveHours; 
    } 

    protected String doInBackground(Bundle... params){ 
     Bundle b = params[0]; 
     String jwt = b.getString("JWT"); 
     System.out.println(jwt); 

     boolean approve = b.getBoolean("APPROVE"); 
     int[] id = b.getIntArray("ID"); 
     try { 
      URL url = new URL("http://voluntu.io/api/hour/update"); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setConnectTimeout(2500 /* milliseconds */); //if i don't do this, it will hang indefinitely 
      httpURLConnection.setReadTimeout(1500 /* milliseconds */); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
      httpURLConnection.setRequestProperty("Host", "voluntu.io"); 
      httpURLConnection.setRequestProperty("Origin", "http://voluntu.io"); 
      httpURLConnection.setRequestProperty("Referer", "http://voluntu.io/hours/approve"); 
      httpURLConnection.setRequestProperty("Cookie", "sessionJWT=" + jwt); 

      httpURLConnection.connect(); 

      JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("approveOrReject", approve); 
      jsonObject.put("hourIDs", Arrays.toString(id)); 

      System.out.println(jsonObject); 

      DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
      wr.writeBytes(jsonObject.toString()); 
      wr.flush(); 
      wr.close(); 

      int HttpResult = httpURLConnection.getResponseCode(); //hangs here 
      System.out.println("HTTP RESULT: " + HttpResult); 
      if(HttpResult == HttpURLConnection.HTTP_OK){ 
       BufferedReader in = new BufferedReader(new InputStreamReader(
         httpURLConnection.getInputStream(), "utf-8" 
       )); 
       String line; 
       while((line = in.readLine()) != null){ 
        sb.append(line); 
       } 
       in.close(); 
      } 
      System.out.println("RESPONSE: " + sb.toString()); 
      httpURLConnection.disconnect(); 
     } 
     catch (MalformedURLException e){ 
      e.printStackTrace(); 
     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 
     catch (JSONException e){ 
      e.printStackTrace(); 
     } 
     return sb.toString(); 
    } 

    protected void onPostExecute(String result){ 
     approveHours.refreshHours(); 
    } 
} 

Es aus irgendeinem Grunde darauf, den HTTP-Response-Code hängt. Ich habe die Header und den Body überprüft und sie sind identisch mit dem, was die Webversion sendet, also habe ich keine Ahnung, warum es nicht funktioniert. Auch das Aufrufen anderer Teile ihrer API funktioniert einwandfrei, und tatsächlich wird dieser Code hauptsächlich aus anderen Teilen meiner App kopiert, die die API aufrufen. Hilfe ist willkommen!

Antwort

0

Ich habe es behoben. Statt eines Arrays müssen Sie JSONArray verwenden, oder das Array wird in Anführungszeichen eingeschlossen, wenn es in das JSON-Objekt eingefügt wird.