2016-10-26 4 views
1

JSONオブジェクトをAsynctaskでWebサービスに渡そうとしています。私はMainActivitycollectStatisticsメソッドを呼び出します。この方法は、モバイルデバイスのいくつかの統計を収集し、JSONオブジェクトでそれらを一緒に組み合わせます。 JSONオブジェクトはAsynctaskに渡されます。AsyncTaskを通じてJSONオブジェクトをWebサービスに渡します

JSONObject jsonProperties = new JSONObject(); 
    try { 
     jsonProperties.put("_device" , _device); 
     jsonProperties.put("_model", _model); 
     jsonProperties.put("_product", _product); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    if (jsonProperties.length() > 0) 
    { 
     //call to async class 
     //String.valueOf(jsonProperties) 
     //new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties); //instead of execute 
     Toast.makeText(context,jsonProperties.toString(),Toast.LENGTH_LONG).show(); 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties); 
      Log.i("msg","calling asynctask"); 
     } else { 
      new SendJsonProperties().execute(jsonProperties); 
      Log.i("msg","NOT calling asynctask"); 
     } 
    } 

Asynctaskは、JSONパラメータ

class SendJsonProperties extends AsyncTask <JSONObject,String,String> 

は私が

protected String doInBackground(JSONObject... params) 
{ 
    //(...) elippsis make the input array 
    Log.i("msg", "do in bkgnd"); 
    WebServicesCaller webServicesCaller = new WebServicesCaller(); 
    result = webServicesCaller.storeStatistics(params[0]); 
    return null; 
} 

doInBackgroundでのWebサービスの呼び出し元のメソッドを呼び出すしかし、私はこのエラー

を得る受け取ります
java.lang.RuntimeException: Cannot serialize: {"_product":"ms013gxx","_model":"SM-G7102","_device":"ms013g"} 

シリアル化の問題を把握することはできません。どんな助けでも大歓迎です。前もって感謝します。

答えて

0

だから私は

jsonProp.type = PropertyInfo.STRING_CLASS; 
request.addProperty(jsonProp, obj.toString()); 

jsonProp.setType(JSONObject.class); 
request.addProperty(jsonProp, obj); 

を変更

0

JSONObjectパラメータのModel POJOクラスを作成する必要があります。 このリンクを使用して、JSON REsponseに従ってPOJOクラスを作成します。私は、文字列パラメータを取るWebメソッド

public string storeStatistics(string jsonObj)

にJSONオブジェクトを送信しようとしていた

http://www.jsonschema2pojo.org/

+0

申し訳ありませんが、私はあなたが何について話しているのか分かりません、もっと説明できますか?ご協力いただきありがとうございます。 –

+0

オブジェクト(_product)がシリアル化されていないため、このエラーが発生します。そのためには、上記のリンクを使用してシリアライズ可能に変換する必要があります。シリアライズされたオブジェクト –

関連する問題