請看下面的內容。他們是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。
這段代碼對我來說很好。 [這裏](http://www.vogella.com/articles/AndroidJSON/article。html)是一篇關於在Android中使用JSON的文章 –
您使用的是什麼json軟件包? – karthi