2016-09-14 146 views
0

所以我從來沒有使用過HTTP後,我需要發送一個響應,並從相同的接收響應。這裏是我提供的文檔:發送HTTP POST和處理JSON響應

編輯:此代碼示例來自Microsoft Azure機器學習,我必須將其集成到我的Android應用程序來做數據預測。

URL

https://**api-version=2.0&details=true 

請求

"Inputs": { 
    "input1": { 
     "ColumnNames": [ 
     "Light", 
     "Proximity", 
     "Ax", 
     "Ay", 
     "Az", 
     "Gx", 
     "Gy", 
     "Gz" 
     ], 
     "Values": [ 
     [ 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0" 
     ], 
     [ 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0", 
      "0" 
     ] 
     ] 
    } 
    }, 
    "GlobalParameters": {} 
} 

樣品響應

{ 
    "Results": { 
    "output1": { 
     "type": "DataTable", 
     "value": { 
     "ColumnNames": [ 
      "Scored Probabilities for Class \"BackPocket\"", 
      "Scored Probabilities for Class \"Ear\"", 
      "Scored Probabilities for Class \"Handbag\"", 
      "Scored Probabilities for Class \"SidePocket\"", 
      "Scored Labels" 
     ], 
     "ColumnTypes": [ 
      "Numeric", 
      "Numeric", 
      "Numeric", 
      "Numeric", 
      "Categorical" 
     ], 
     "Values": [ 
      [ 
      "0", 
      "0", 
      "0", 
      "0", 
      "BackPocket" 
      ], 
      [ 
      "0", 
      "0", 
      "0", 
      "0", 
      "BackPocket" 
      ] 
     ] 
     } 
    } 
    } 
} 

如何以Java/Android方式發送此Post Request,然後處理響應?

我完全陌生,因此我不知道。我沒有搜索HTTP POST等,但無法找到足夠的發送和接收響應。任何圖書館的建議也非常感謝。

謝謝。

+0

您使用的是Android上的任何HTTP庫? – Panther

+0

@Panther無。此代碼示例來自Microsoft Azure機器學習,我必須將其集成到我的Android應用程序中才能進行數據預測。 –

+0

[在Android中發送POST數據]可能的重複(http://stackoverflow.com/questions/2938502/sending-post-data-in-android) –

回答

2

有很多HTTP連接庫。你可以用他們任何一個:

  • OkHttp - 非常強大和簡單的庫。你可以找到很多關於它的page

  • 改造圖書館的例子自帶OkHttp和包裝要求到接口Retrofit Link

爲了對付JSON,我建議你使用GSON library

所以處理您的問題的基本算法如下:

  • 用gett創建GSON模型文件ERS和setter
  • 與方法創建HTTP調用你需要
  • 撥打電話和接收請求(這樣做不是在UI線程)
  • 與創建getter方法解析請求/ setter方法
1

下面是一個例子請求使用Google的Volley作爲http請求,使用Gson將json數據轉換爲對象。如果您打算使用Gson,則需要創建表示json數據的pojos。

你輸入的數據是相當混亂,我沒有嘗試轉換,從一個POJO,那是讀者的練習;)

String request = "{\"Inputs\":{\"input1\":{\"ColumnNames\":[\"Light\",\"Proximity\",\"Ax\",\"Ay\",\"Az\",\"Gx\",\"Gy\",\"Gz\"],\"Values\":[[\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]]}},\"GlobalParameters\":{}}"; 

     Context appContext = InstrumentationRegistry.getTargetContext(); 
     String url ="https://**api-version=2.0&details=true"; 

     RequestQueue queue = Volley.newRequestQueue(appContext); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         Log.d("TAG", "success: " + response); 
         String jsonResponse = response; 
         Gson gson = new Gson(); 
         Stuff stuffObject = gson.fromJson(jsonResponse, Stuff.class); 

        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 

     queue.add(stringRequest); 

POJO的對象與jsonSchemaToPojo

Stuff.class產生

public class Stuff { 

    @SerializedName("Results") 
    @Expose 
    private Results results; 

    public Results getResults() { 
     return results; 
    } 
    public void setResults(Results results) { 
     this.results = results; 
    } 
} 

Results.class

public class Results { 

    @SerializedName("output1") 
    @Expose 
    private Output1 output1; 

    public Output1 getOutput1() { 
     return output1; 
    } 

    public void setOutput1(Output1 output1) { 
     this.output1 = output1; 
    } 
} 

Output1.class

public class Output1 { 

    @SerializedName("type") 
    @Expose 
    private String type; 
    @SerializedName("value") 
    @Expose 
    private Value value; 

    public String getType() { 
     return type; 
    } 
    public void setType(String type) { 
     this.type = type; 
    } 
    public Value getValue() { 
     return value; 
    } 
    public void setValue(Value value) { 
     this.value = value; 
    } 
} 

Value.class

public class Value { 

    @SerializedName("ColumnNames") 
    @Expose 
    private List<String> columnNames = new ArrayList<String>(); 
    @SerializedName("ColumnTypes") 
    @Expose 
    private List<String> columnTypes = new ArrayList<String>(); 
    @SerializedName("Values") 
    @Expose 
    private List<List<String>> values = new ArrayList<List<String>>(); 

    public List<String> getColumnNames() { 
     return columnNames; 
    } 
    public void setColumnNames(List<String> columnNames) { 
     this.columnNames = columnNames; 
    } 
    public List<String> getColumnTypes() { 
     return columnTypes; 
    } 
    public void setColumnTypes(List<String> columnTypes) { 
     this.columnTypes = columnTypes; 
    } 
    public List<List<String>> getValues() { 
     return values; 
    } 
    public void setValues(List<List<String>> values) { 
     this.values = values; 
    } 

} 
+0

排除已被棄用,所以我不認爲它是一個好主意,推薦它 –

+0

@AntonKazakov你有這個索賠的參考?我使用'compile'c​​om.android.volley:volley:1.0.0''並且沒有看到棄用警告。 –

+0

也許我錯了。我很久以前就使用過排序,上次我在github mcxiaoke/android-volley上打開它的回購,它說它已被棄用。但如果你從谷歌編譯它的意思是我錯了。但無論如何OkHttp庫做得更好:) –