如何调试" ModelClass无法映射到JSON对象。也许它没有用@JsonObject注释?"

时间:2016-04-06 09:09:48

标签: android json

我想实现JsonObject但是当我喜欢下面的代码错误时会收到错误..

04-06 14:26:55.436 32618-460/com.example.muhammad.try1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
 Process: com.example.muhammad.try1, PID: 32618
 java.lang.RuntimeException: An error occured while executing doInBackground()
     at android.os.AsyncTask$3.done(AsyncTask.java:300)
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
     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:818)
  Caused by: com.bluelinelabs.logansquare.NoSuchMapperException: Class com.example.muhammad.try1.ModelClass could not be mapped to a JSON object. Perhaps it hasn't been annotated with @JsonObject?
     at com.bluelinelabs.logansquare.LoganSquare.mapperFor(LoganSquare.java:304)
     at com.bluelinelabs.logansquare.LoganSquare.parse(LoganSquare.java:79)
     at com.example.muhammad.try1.MainActivity$JSONAsyncTask.doInBackground(MainActivity.java:298)
     at com.example.muhammad.try1.MainActivity$JSONAsyncTask.doInBackground(MainActivity.java:253)
     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:818) 

和此行上的错误点,

ModelClass s = LoganSquare.parse(jsonarray.getJSONObject(i).toString(), ModelClass.class);

我添加了这个依赖

compile 'com.bluelinelabs:logansquare:1.3.6'

请帮我解决此错误

@JsonObject
public class ModelClass {

 @JsonField(name = "longi")
public String longitudeServer;

@JsonField(name = "lati")
public String latitudeServer;

@JsonField(name = "uniqueid")
 public String uniqueidSserver;

public ModelClass(){
}
}

这是我的DoInBacground函数,我收到错误

 @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>

            HttpGet httpGet = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httpGet);
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONArray jsonarray = new JSONArray(data);

                latLngList.clear();

                for (int i = 0; i < jsonarray.length(); i++) {
                    ModelClass s = LoganSquare.parse(jsonarray.getJSONObject(i).toString(), ModelClass.class);

                    LatLng latLng = new LatLng(Double.parseDouble(s.getLatitude()), Double.parseDouble(s.getLongitude())); // Use your server's methods
                    latLngList.add(latLng);

                }


                return true;

            }

            //------------------>>

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;


    }

1 个答案:

答案 0 :(得分:1)

其他方法:

 for (int i = 0; i < jsonarray.length(); i++) {
   JSONObject jsonModel = jsonarray.getJSONObject(i)
    String longi = jsonModel.getString("longi");
    String lati = jsonModel.getString("lati");
    String uniqueid = jsonModel.getString("uniqueid");

    LatLng latLng = new LatLng(Double.parseDouble(lati), Double.parseDouble(longi));
    latLngList.add(latLng);

}

修改

build.gradle内添加依赖关系compile 'com.bluelinelabs:logansquare-compiler:1.3.6',如下所示:

dependencies {
....
compile 'com.bluelinelabs:logansquare-compiler:1.3.6'
....
}