2013-12-18 23 views
0

請看下面的內容。他們是JSON格式。不要擔心用戶名和ID。這是假的。您可以看到鏈接here在android中創建JSON

身份驗證

POST /andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730; HTTP/1.1 
Host: bigml.io 
Content-Type: application/json 

創建預測

curl https://bigml.io/andromeda/prediction?$BIGML_AUTH \ 
-X POST \ 
-H 'content-type: application/json' \ 
-d '{"model": "model/50a2eac63c19200bd1000008", "input_data": {"000001": 3}}' 

以上,BIGML_AUTH指一個變量,包括用戶名和API密鑰。

現在,我要用android做這些JSON調用。下面是我的代碼

private JSONObject putJson() 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     JSONObject finalResult = null; 


     HttpPost httppost = new HttpPost("https://bigml.io/andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730"); 
     httppost.setHeader("Content-type", "application/json; charset=utf-8"); 

     JSONObject json = new JSONObject(); 

     try { 
      // Add your data 
      json.put("model","model/50a2eac63c19200bd1000008"); 
      json.put("input_data", "{000001:3}"); 
      StringEntity se = new StringEntity(json.toString()); 
      httppost.setEntity(se); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          response.getEntity().getContent(), "UTF-8")); 
      String jsonString = reader.readLine(); 
      JSONTokener tokener = new JSONTokener(jsonString); 

      finalResult = new JSONObject(tokener); 
     } 
     catch(Exception e) 
     { 
      Log.d("Error here", "Error is here",e); 
     } 

     return finalResult; 

    } 

我需要知道我是否正確編碼在Android中給出Json。我無法運行並檢查,因爲在運行和檢查時,它會返回ID不存在錯誤。

請讓我知道我是否已經正確編碼了JSON,因爲我之前沒有使用過JSON。

+0

這段代碼對我來說很好。 [這裏](http://www.vogella.com/articles/AndroidJSON/article。html)是一篇關於在Android中使用JSON的文章 –

+0

您使用的是什麼json軟件包? – karthi

回答

2

有你的代碼和捲曲略有區別:

你代碼:

 // Add your data 
     json.put("model","model/50a2eac63c19200bd1000008"); 
     json.put("input_data", "{000001:3}"); 

如果要產生相同的JSON作爲您curl命令,它應該閱讀:

 // Add your data 
     json.put("model","model/50a2eac63c19200bd1000008"); 
     JSONObject input_data = new JSONObject(); 
     input_data.put("000001", 3); 
     json.put("input_data", input_data); 

我遇到了另一件可能導致問題的東西。你只讀了你的迴應的第一行。在很多情況下,這不是問題,因爲大多數JSON對象都是單行的。但是,如果有人在API服務器上啓用DEBUG模式,則可能會發生JSON對象出現在多行(又名人類可讀)的情況。開始使用DefaultHttpClient的Android版本> FROYO(使約97%的手機)。對於GINGERBREAD和更新版本,我們應該使用HttpUrlConnection來請求Web資源和REST/JSON。

我製作了一個名爲DavidWebb的小型庫,它使得在發出HTTP請求時更輕鬆。使用該庫的代碼應該是這樣的:

private JSONObject putJson() throws Exception { 
    JSONObject result = null; 
    try { 
     JSONObject json = new JSONObject(); 

     // Add your data 
     json.put("model", "model/50a2eac63c19200bd1000008"); 
     JSONObject input_data = new JSONObject(); 
     input_data.put("000001", 3); 
     json.put("input_data", input_data); 

     Webb webb = Webb.create(); 
     result = webb 
       .post("https://bigml.io/andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730") 
       .body(json) 
       .ensureSuccess() 
       .asJsonObject() 
       .getBody(); 

    } catch (JSONException e) { 
     // should not happen 
     // Log.d("Error here", "Error is here",e); 
    } catch (WebbException e) { 
     // carries the real exception 
    } 
    return result; 
} 

我提供的鏈接給出了針對Android HTTP請求庫許多選擇 - 你不必使用DavidWebb。

+0

謝謝。爲什麼我會得到'FileNotFoundException'?當我使用你的lib? –

+0

可能有一個'404 Not Found'結果。你能把你的堆棧跟蹤傳給我嗎?你可以在GitHub上創建一個問題。對我來說,它不工作 - 我得到一個java.net.HttpRetryException。 – hgoebl

+0

如果您搜索我的用戶名或解密我的個人資料中的聯繫人,您可以與我聯繫。 – hgoebl

1

org.json.JSONObject?

如果是這樣,那麼在這裏你的json代碼是好的&也會給有效的json呢!

{ 「模式」: 「模型/ 50a2eac63c19200bd1000008」, 「input_data」: 「{000001:3}」 }

+0

感謝您的回覆。 +1從mw –