【问题标题】:How to Implement GSON @SerializedName I received errors in DoInBackground如何实现 GSON @SerializedName 我在 DoInBackground 中收到错误
【发布时间】:2016-04-06 11:29:38
【问题描述】:

如何实现 GSON @SerializedName 我在 ModelClass modelClass = new Gson().fromJson(response, ModelClass.class); 中收到错误 我卡住了帮我做什么?

错误是

原因:java.lang.ClassCastException:org.apache.http.message.BasicHttpResponse 无法转换为 java.io.Reader 在 com.example.muhammad.try1.MainActivity$JSONAsyncTask.doInBackground(MainActivity.java:300) 在 com.example.muhammad.try1.MainActivity$JSONAsyncTask.doInBackground(MainActivity.java:254)

public class ModelClass {

@SerializedName("longi")
public String longitudeServer;

@SerializedName("lati")
public String latitudeServer;

@SerializedName("uniqueid")
public String uniqueidSserver;

public ModelClass(){
}

public String getLongitude(){
    return longitudeServer;
}


public String getLatitude(){
    return latitudeServer;
}


public String getUniqueId(){
    return uniqueidSserver;
}


//... More setter and getter here
}

我在 DoInBackground 中收到错误

    @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 modelClass = new Gson().fromJson(response, ModelClass.class);

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

                }


                return true;

            }

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

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


    }`

【问题讨论】:

    标签: android performance shell android-layout android-fragments


    【解决方案1】:
    ModelClass modelClass = 
       new Gson().fromJson(jsonarray.getJSONObject(i).toString(), ModelClass.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多