2016-06-14 15 views
-1

現在、アンドロイドのwebserviceに接続する際に問題があります。ジャクソンコア/データバインド/アノテーション2.2.4とSpring RESTWebServiceを使用しています。私は、ブラウザからURLにアクセスした場合、私はJSONレスポンスを見ることができます:(サーバー復帰リスト\ショップ\は:)クライアントエンドポイント(Androidアプリ)からSTART_ARRAYトークンからパッケージのインスタンスを逆シリアル化できません

[{"id":1,"product_tmpl_id":1,"message_last_post":null,"ean13":null,"default_code":null,"name_template":"Service","image_variant":null,"active":true,"create_uid":1,"create_date":1463633587963,"write_date":1463633587963,"write_uid":1}] 

のように見える私は、このエラーメッセージが表示されます。

06-14 15:18:34.702 14597-14639/? E/MainActivity: Could not read JSON: Can not deserialize instance of stpi.com.rest.Products out of START_ARRAY token 
at [Source: co[email protected]41da0398; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of stpi.com.rest.Products out of START_ARRAY token 
at [Source: com.android.okhttp.internal.http.HttpTransport$ChunkedIn[email protected]; line: 1, column: 1] 
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of stpi.com.rest.Products out of START_ARRAY token 
at [Source: co[email protected]41da0398; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of stpi.com.rest.Products out of START_ARRAY token 
at [Source: co[email protected]41da0398; line: 1, column: 1] 
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126) 
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147) 
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76) 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484) 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439) 
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237) 
at stpi.com.rest.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:83) 
at stpi.com.rest.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:76) 
at android.os.AsyncTask$2.call(AsyncTask.java:288) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of stpi.com.rest.Products out of START_ARRAY token 
at [Source: co[email protected]41da0398; line: 1, column: 1] 
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691) 
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685) 
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1215) 
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:151) 
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126) 
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993) 
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158) 
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:123) 
... 13 more 
06-14 15:18:34.707 14597-14597/? E/AndroidRuntime: FATAL EXCEPTION: main 
Process: stpi.com.rest, PID: 14597 
java.lang.NullPointerException 
at stpi.com.rest.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:96) 
at stpi.com.rest.MainActivity$HttpRequestTask.onPostExecute(MainActivity.java:76) 
at android.os.AsyncTask.finish(AsyncTask.java:632) 
at android.os.AsyncTask.access$600(AsyncTask.java:177) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
Androidアプリで

私の主な活動:

package stpi.com.rest; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 
import org.springframework.web.client.RestTemplate; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()) 
        .commit(); 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     new HttpRequestTask().execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_refresh) { 
      new HttpRequestTask().execute(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.content_main, container, false); 
      return rootView; 
     } 
    } 


    private class HttpRequestTask extends AsyncTask<Void, Void, Products> { 
     @Override 
     protected Products doInBackground(Void... params) { 
      try { 
       final String url = "http://164.164.36.11:443/seis/productController/getProduct"; 
       RestTemplate restTemplate = new RestTemplate(); 
       restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 
       Products greeting = restTemplate.getForObject(url, Products.class); 
       return greeting; 
      } catch (Exception e) { 
       Log.e("MainActivity", e.getMessage(), e); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Products greeting) { 
      TextView greetingIdText = (TextView) findViewById(R.id.id_value); 
      TextView greetingContentText = (TextView) findViewById(R.id.content_value); 
      greetingIdText.setText(greeting.getId()); 
      greetingContentText.setText(greeting.getname_template()); 
      greetingContentText.setText(greeting.getproduct_tmpl_id()); 
      greetingContentText.setText(greeting.getmessage_last_post()); 
      greetingContentText.setText(greeting.getean13()); 
      greetingContentText.setText(greeting.getdefault_code()); 
      greetingContentText.setText(greeting.getimage_variant()); 
      greetingContentText.setText(greeting.getactive()); 
      greetingContentText.setText(greeting.getcreate_uid()); 
      greetingContentText.setText(greeting.getcreate_date()); 
      greetingContentText.setText(greeting.getwrite_date()); 
      greetingContentText.setText(greeting.getwrite_uid()); 
     } 

    } 

} 

これは私がID、残りの変数とゲッターを追加他のクラスの製品です。

package stpi.com.rest; 

/** 
* Created by Admin on 6/14/2016. 
*/ 
public class Products { 

    private String id; 
    private String name_template; 
    private String product_tmpl_id; 
    private String message_last_post; 
    private String ean13; 
    private String default_code; 
    private String image_variant; 
    private String active; 
    private String create_uid; 
    private String create_date; 
    private String write_date; 
    private String write_uid; 


    public String getId() { 
     return this.id; 
    } 

    public String getname_template() { 
     return this.name_template; 
    } 

    public String getproduct_tmpl_id() { 
     return this.product_tmpl_id; 
    } 

    public String getmessage_last_post() { 
     return this.message_last_post; 
    } 

    public String getean13() { 
     return this.ean13; 
    } 

    public String getdefault_code() { 
     return this.default_code; 
    } 

    public String getimage_variant() { 
     return this.image_variant; 
    } 
    public String getactive() { 
     return this.active; 
    } 

    public String getcreate_uid() { 
     return this.create_uid; 
    } 

    public String getcreate_date() { 
     return this.create_date; 
    } 

    public String getwrite_date() { 
     return this.write_date; 
    } 

    public String getwrite_uid() { 
     return this.write_uid; 
    } 



} 

この問題を解決するお手伝いはありますか?

+1

単一オブジェクトとオブジェクトコレクションの違いを知っていますか? JSONでどのように表現されていますか? – Selvin

+0

'Products'のリストを取得していますが、そのリストをObjectに割り当てようとしています。だからあなたはエラーが発生している – SripadRaj

+0

実際に私はこの環境を抱えています。 –

答えて

1

単一のオブジェクトを取得するためにオブジェクトコレクションが取得されているので、Product []を使用して正常に動作しました。

private class HttpRequestTask extends AsyncTask<Void, Void, Products> { 
     Products[] greeting; 
     @Override 
     protected Products doInBackground(Void... params) { 
      try { 
       final String url = "http://164.164.36.11:443/seis/productController/getProduct"; 
       RestTemplate restTemplate = new RestTemplate(); 
       restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 
       greeting = restTemplate.getForObject(url, Products[].class); 

       return greeting[0]; 
      } catch (Exception e) { 
       Log.e("MainActivity", e.getMessage(), e); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Products greeting) { 
      TextView greetingIdText = (TextView) findViewById(R.id.id_value); 
      TextView greetingContentText = (TextView) findViewById(R.id.content_value); 
      Log.i("post exe active", greeting.getname_template()); 
      greetingIdText.setText(greeting.getId()); 
      greetingContentText.setText(greeting.getname_template()); 
関連する問題