2016-09-12 51 views
0

APIを使用してJavaに初心者です。JSONArrayの初期値は文字列またはコレクションまたは配列である必要があります

次のように私は私のプログラム中に取得していますそのレスポンス:

*オブジェクトの応答:

{ 
     "statusLine":{ 
      "reasonPhrase":"OK", 
      "protocolVersion":{}, 
      "statusCode":200 
     }, 
     "protocolVersion":{}, 
     "locale":"en_IN", 
     "entity":{ 
      "repeatable":false, 
      "content":{} 
     } 
    } 

例外が発生しました。 JSONArrayの初期値は、文字列またはコレクションまたは配列である必要があります。*

結果に完全な値を取得できません。私はヘッダーの詳細だけを取得しています。

APIの詳細をご覧ください。 JAVAを使用する

package IOT; 

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonParser; 
import java.util.HashMap; 
import java.util.Map; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicHeader; 
import org.apache.http.protocol.HTTP; 
import org.json.JSONObject; 
import org.json.JSONArray; 

public class HttpPostWithBody { 

public static void main(String args[]) { 
    String Message = "6f2159f998"; 

    try { 
     new HttpPostWithBody().sendJSONData(Message); 
    } catch (Exception E) { 
     System.out.println("Exception Occured. " + E.getMessage()); 
    } 
} 

public String sendJSONData(String message) throws Exception { 

    Map< String, Object> jsonValues = new HashMap< String, Object>(); 
    { 
    jsonValues.put("thing_key", message); 
    jsonValues.put("from", "2016/08/29 16:55:00"); 
    jsonValues.put("to", "2016/08/29 17:05:00"); 
    jsonValues.put("time_zone", "Mumbai"); 
    jsonValues.put("time_format", "str"); 
    jsonValues.put("per", 50); 
    //jsonValues.put("metrics", "1st data"); 
    } 
    JSONObject json = new JSONObject(jsonValues); 

    String url = "https://api.datonis.io/api/v3/datonis_query/thing_data"; 

    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 
    post.setHeader("Content-Type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
    post.setHeader("X-Auth-Token", "YZ0Sa7IfpdtAXQQ-2CNeAg"); 

    StringEntity entity = new StringEntity(json.toString(), "UTF8"); 
    System.out.println("This is the entity: " + entity); 

    Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
    JsonParser jp = new JsonParser(); 
    JsonElement je = jp.parse(json.toString()); 
    String prettyJsonString = gson.toJson(je); 
    System.out.println("Pretty Json: " + prettyJsonString); 

    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 

    post.setEntity(entity); 
    //this is your response: 

    HttpResponse response = client.execute(post); 

    JSONObject jo = new JSONObject(response); 
    System.out.println("Object Response: " + jo); 

    JSONArray ab = new JSONArray(response); 
    JSONObject cd = ab.getJSONObject(0); 

    System.out.println("Array Response: " + cd); 
    System.out.println("Array Response: " + jo.getJSONArray("6f2159f998")); 
    System.out.println("Connection Status: " + response.getStatusLine()); 
    return response.getStatusLine().toString(); 

    } 
} 

答えて

0

まず、応答データが有効なJSONデータであることを確認する必要があります。いくつかのJSONチェックサイトでは、jsonが有効であることを簡単に見つけることができます。フォーマット後: { "statusLine": { "reasonPhrase": "OK", "protocolVersion": {}, "statusCode": 200 }, "protocolVersion": {}, "locale": "en_IN", "entity": { "repeatable": false, "content": {} } } 問題は、このJSONデータがJSON Objectであり、JSON Arrayではないということです。したがって、文JSONArray ab = new JSONArray(response);はエラーを引き起こします。あなたがstatusLine結果を取得したい場合は、以下のコードを使用することができます JSONObject jo = new JSONObject(response); String statusLine = jo.getJSONObject("statusLine").toString();

+0

をあなたはこのしてください –

+0

@GaneshRavikumarのための任意の完全なプログラムの参照を持っています申し訳ありませんが、完全なプログラムの参照がありません。多分あなたはJava JSONのようなキーワードを使ってgoogleに行き、Java JSONを十分に理解しているかもしれません。 JSONデータの '' {}}はJavaのJSONObjectで表現でき、 '' [] ''はJavaのJSONArrayで表現できるということがわかりやすいでしょう。 – HaoChih

+0

ステータス行だけを取得しています。表示する必要のあるデータがあります。このプログラムでは表示されません。親切に私を助けてください。 –

関連する問題