2016-12-14 8 views
0

私は現在開発中で、ASP.NET WebApiサービスからデータを収集するアンドロイドアプリです。サービスは動作しますが、私はアンドロイドでJSONオブジェクトを取得できません.JSON配列またはJSONオブジェクトに変換する必要がありますが、私は答えはたくさんありましたが、成功しませんでした。JSONをWebApiからAndroidのJson Objectに変換できません

// GET api/uconnectservice 
     public String Get() 
     { 

      var json = JsonConvert.SerializeObject(

       sampledata.GetAllDummy() 
      ); 
      return json; 

      //return sampledata.GetAll(); 
     } 

できます:ここに私のサーバー側のコードスニペットで

私はここに、このサービスを消費する必要がアンドロイド側で

結構です

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> 
    [{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"15/12/2016 22:35:03","ValueDate":"15/12/2016 22:35:03"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"16/12/2016 22:35:03","ValueDate":"16/12/2016 22:35:03"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"17/12/2016 22:35:03","ValueDate":"17/12/2016 22:35:03"}......MORE JSON.........{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"20/12/2016 22:35:03","ValueDate":"20/12/2016 22:35:03"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"21/12/2016 22:35:03","ValueDate":"21/12/2016 22:35:03"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"22/12/2016 22:35:03","ValueDate":"22/12/2016 22:35:03"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"23/12/2016 22:35:03","ValueDate":"23/12/2016 22:35:03"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"24/12/2016 22:35:03","ValueDate":"24/12/2016 22:35:03"}]}] 
</string> 

などは、私のコードは次のとおりです。

String URL = "http://192.168.1.101/UnicsApplication/api/uconnectservice"; 

    JSONObject jsonObject; 
    private final OkHttpClient client = new OkHttpClient(); 

    public void run() throws Exception { 
     Request request = new Request.Builder().url(URL).build(); 

     client.newCall(request).enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       e.printStackTrace(); 

      } 
try (ResponseBody responseBody = response.body()) { 

        if (!response.isSuccessful()) 

         throw new IOException("Unexpected code " + response); 

        Headers responseHeaders = response.headers(); 

        for (int i = 0, size = responseHeaders.size(); i < size; i++) { 

         Log.d("Results", responseHeaders.name(i) + ": " + responseHeaders.value(i)); 

        } 
        //Log.d("Results", responseBody.string()); 

        try { 
         //get JSON objetc first 

         jsonObject = new JSONObject(responseBody.string()); 

         Log.d("JsonObject", jsonObject.toString()); 

        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

e.printstackTrance方法は、私はLogcatがあるから得続けるcalled.ErrorメッセージですSは次のとおりです。

12-15 01:59:44.280: W/System.err(26192): org.json.JSONException: Value {"operations":[{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I need money right now","Debit":"5004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I need money right now","Debit":"5005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"150010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/20 
12-15 01:59:44.280: W/System.err(26192): at org.json.JSON.typeMismatch(JSON.java:111) 
12-15 01:59:44.280: W/System.err(26192): at org.json.JSONObject.<init>(JSONObject.java:159) 
12-15 01:59:44.280: W/System.err(26192): at org.json.JSONObject.<init>(JSONObject.java:172) 
12-15 01:59:44.280: W/System.err(26192): at com.nickSoft.Connections.OkHttpHandlerIncoming$1.onResponse(OkHttpHandlerIncoming.java:67) 
12-15 01:59:44.280: W/System.err(26192): at okhttp3.RealCall$AsyncCall.execute(RealCall.java:126) 
12-15 01:59:44.280: W/System.err(26192): at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
12-15 01:59:44.280: W/System.err(26192): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-15 01:59:44.280: W/System.err(26192): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-15 01:59:44.280: W/System.err(26192): at java.lang.Thread.run(Thread.java:841) 

これはJSON文字列ですか?後でコレクションに変換されるJSONオブジェクトを取得するにはどうしたらよいですか?ご指導や助力をお願いします。 Thnx。

+0

リクエストにacceptヘッダ:accept:application/jsonを追加してみてください。これにより、サーバーはXML文字列ではなくJSONを返すようになります。編集:サーバー側でオブジェクトをシリアル化するか、文字列を返す必要はありません。どのようなオブジェクトであれ返すだけで、アカウントやリストのように見えるかもしれません。、WebApiがシリアライゼーションを手助けします。 – Borophyll

+0

ok @Borophyllはこれをしようとしています。私は少し混乱しています。 – user3015410

+0

あなたがあなたのapi(アンドロイド側)にリクエストを提出するコードを投稿すれば、あなたがすでに試したものを見ることができます。 – Borophyll

答えて

0

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">

は、あなたが最初にJSONが含まれているいくつかのXMLを得たように見えますが、有効なJSONではありません。私の提案は、サーバー側で正しく削除することです。それが不可能な場合は、構文解析を開始する前にレスポンスから文字列タグを削除してください。

+0

thnks @QVDevこのコードを追加しました:config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue( "text/html"));私のlogcatがこのエラーをどのように表示するかを知っています。質問は編集を参照してください..... – user3015410

+0

https://jsonformatter.curiousconcept.com/で応答を検証してください。有効なJSONを返すようにしてくださいサーバー上で – QVDev

関連する問題