2017-07-11 3 views
-4

Ich benutze Post-Man, um die URL (Post) zu testen, es sieht gut aus. API: http://www.dmapp.com.tw/MobileApp/GetHealthData.phpWarum kann ich die JSON-Daten nicht bekommen?

Aber wenn ich es auf meinem Android versuchen, bekomme ich die Meldung nicht einmal mein Antwortcode 200

Mein logcat zeigt {"Msg":"Request method not accepted","JsonData":null,"ErrorCode":"0099"}

Ich verstehe es nicht, ich verwenden der Code, um die JSON-Daten lange vorher zu bekommen.

Einige können mir beibringen, welchen Schritt ich vermisse, das wäre zu schätzen.

Hier ist meine get json Datenfunktion:

private String getRoute(String url) throws IOException { 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     int responseCode = connection.getResponseCode(); 
     StringBuilder jsonIn = new StringBuilder(); 
     // responseCode show 200 
     Log.d("responseCode:",responseCode+""); 
     if (responseCode == 200) { 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       jsonIn.append(line); 
      } 
     } else { 
      Log.d(TAG, responseCode + "responseCode"); 
     } 
     connection.disconnect(); 
     // I can't get the json data it shows Request method not accepted 
     Log.d(TAG, jsonIn + "jsonIn"); 
     return jsonIn.toString(); 

    } 
+0

Der Link, den Sie verknüpft haben, hat diese er ror Nachricht auch. – andras

+0

Danke ich bekomme jetzt die Antwort. –

Antwort

2

Die HttpURLConnection sendet standardmäßig eine GET-Anfrage.
Ihr Link funktioniert einwandfrei.
Try
connection.setRequestMethod("POST");

Reference

+0

Danke Sam! Du rettest mich ! –

1

Die Antwort vom Server selbst null als JsonData ist, sollten Sie auf Server-Seite überprüfen.

1

Hallo Ihr Link-arbeitet sich nur, dass ihr ein Beitrag Antrag

private String getRoute(String url) throws IOException { 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     int responseCode = connection.getResponseCode(); 
     connection.setRequestMethod("POST"); 


     StringBuilder jsonIn = new StringBuilder(); 
     // responseCode show 200 
     Log.d("responseCode:",responseCode+""); 
     if (responseCode == 200) { 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       jsonIn.append(line); 
      } 
     } else { 
      Log.d(TAG, responseCode + "responseCode"); 
     } 
     connection.disconnect(); 
     // I can't get the json data it shows Request method not accepted 
     Log.d(TAG, jsonIn + "jsonIn"); 
     return jsonIn.toString(); 

    } 

Ich hoffe, es funktioniert aber meiner Meinung nach Zugabe sollten Sie arbeiten mit Volley oder Retrofit anstelle von HttpURLConnection

Verwandte Themen