Android:使用Retrofit获取数据列表

时间:2016-04-14 05:19:24

标签: android web-services android-studio retrofit

我正在尝试使用Retrofit实现Fetch数据列表,它不起作用,它显示空白屏幕,我无法获取数据,我的Json响应如下

    [
            {
                "code": 1,
                "message": "listed!",
                "totalrecord": 2,
                "result": [
                    {
                        "FieldTypeID": "2",
                        "Name": "Marketing"
                    },
                    {
                        "FieldTypeID": "60",
                        "Name": "Designing"
                    },
                    {
                        "FieldTypeID": "61",
                        "Name": "SEO"
                    },
                    {
                        "FieldTypeID": "78",
                        "Name": "Mobile App Development"
                    }
       ]
    }
]

MainActivity.java

 public class MainActivity extends ListActivity {
        ArrayList<Result> flowerList;

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

//        final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://services.hanselandpetal.com").build();
        final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint(BASE_URL).build();

        api flowerapi = restadapter.create(api.class);

        flowerapi.getData(new Callback<ArrayList<Result>>() {
            @Override
            public void success(ArrayList<Result> flowers, Response response) {
                flowerList = flowers;
                Log.d("ArrayList:: :: :::", String.valueOf(flowerList.size()));
                adapter adapt = new adapter(getApplicationContext(), R.layout.item_file, flowerList);
                //ListView listView = (ListView) findViewById(R.id.list);
                setListAdapter(adapt);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

接口是api.java

    public interface api {

//    @GET("/feeds/flowers.json")
    @GET("/GetUserGroupList")
    public void getData(Callback<ArrayList<Result>> response);
}

Result.Java

 public class Result {
    @SerializedName("FieldTypeID")
    @Expose
    private String FieldTypeID;
    @SerializedName("Name")
    @Expose
    private String Name;

    /**
     *
     * @return
     * The FieldTypeID
     */
    public String getFieldTypeID() {
        return FieldTypeID;
    }

    /**
     *
     * @param FieldTypeID
     * The FieldTypeID
     */
    public void setFieldTypeID(String FieldTypeID) {
        this.FieldTypeID = FieldTypeID;
    }

    /**
     *
     * @return
     * The Name
     */
    public String getName() {
        return Name;
    }

    /**
     *
     * @param Name
     * The Name
     */
    public void setName(String Name) {
        this.Name = Name;
    }
}

Flower.java

public class Flower {
    @SerializedName("code")
    @Expose
    private Integer code;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("totalrecord")
    @Expose
    private Integer totalrecord;
    @SerializedName("result")
    @Expose
    private List<Result> result = new ArrayList<Result>();

    /**
     *
     * @return
     * The code
     */
    public Integer getCode() {
        return code;
    }

    /**
     *
     * @param code
     * The code
     */
    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     *
     * @return
     * The message
     */
    public String getMessage() {
        return message;
    }

    /**
     *
     * @param message
     * The message
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     *
     * @return
     * The totalrecord
     */
    public Integer getTotalrecord() {
        return totalrecord;
    }

    /**
     *
     * @param totalrecord
     * The totalrecord
     */
    public void setTotalrecord(Integer totalrecord) {
        this.totalrecord = totalrecord;
    }

    /**
     *
     * @return
     * The result
     */
    public List<Result> getResult() {
        return result;
    }

    /**
     *
     * @param result
     * The result
     */
    public void setResult(List<Result> result) {
        this.result = result;
    }

}

请帮帮我!!!

日志:

04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- HTTP 200 http://203.124.107.14:39/VrinsoftCRM.asmx/GetUserGroupList (242ms)
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Cache-Control: private, max-age=0
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Content-Length: 699
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Server: Microsoft-IIS/7.5
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-AspNet-Version: 2.0.50727
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-Powered-By: ASP.NET
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Date: Thu, 14 Apr 2016 09:48:54 GMT
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Selected-Protocol: http/1.1
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Sent-Millis: 1460627392316
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Received-Millis: 1460627392436
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ [{"code":1,"message":"listed!","totalrecord":2,"result":[{"FieldTypeID": "2","Name": "Marketing" },{"FieldTypeID": "60","Name": "Designing" },{"FieldTypeID": "61","Name": "SEO" },{"FieldTypeID": "78","Name": "Mobile App Development" },{"FieldTypeID": "79","Name": ".Net Development" },{"FieldTypeID": "81","Name": "QA" },{"FieldTypeID": "94","Name": "HR" },{"FieldTypeID": "100","Name": "Business Analyst" },{"FieldTypeID": "101","Name": "Business Development Executive" },{"FieldTypeID": "131","Name": "Network Engg" },{"FieldTypeID": "142","Name": "Facebook" },{"FieldTypeID": "156","Name": "PHP Development" },{"FieldTypeID": "157","Name": "Google" },{"FieldTypeID": "158","Name": "Google 2" }]}]
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- END HTTP (699-byte body)
04-14 05:49:52.434    1375-1375/com.example.android.listviewretrofit D/Suceess :: ::﹕ [ 04-14 05:49:52.434  1375: 1375 D/mListResult size :: :: ]
    1

2 个答案:

答案 0 :(得分:0)

您需要创建一个帮助类

    $test = collect($categories)->map(function ($q) {
        return collect($q)->lists('id')->toArray();
    });

然后改变api方法:

public class Result{
   public int code;
   public String message;
   public int totalrecord;
   public List<Flower> result;
}

答案 1 :(得分:0)

请尝试以下代码

创建类ApiService.class

public class ApiService {
private static final String TAG = "__API__Service";

private final api mApiService;

public ApiService(Context context) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
            .create();
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(BASE_URL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setClient(new OkClient(okHttpClient))
            .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
            .setLog(new AndroidLog(TAG))
            .setConverter(new CleanGsonConverter(gson))
            .setErrorHandler(new CustomErrorHandler(context))
            .build();
    this.mApiService = restAdapter.create(api.class);
}

   public List<Result> getAllDataFromServer() {
    if (mApiService != null) {
        mApiService.getAllData();
    }
}
}

创建CleanGsonConverter.class

public class CleanGsonConverter extends GsonConverter {

private Gson mGson;

public CleanGsonConverter(Gson gson) {
    super(gson);
    mGson = gson;
}

public CleanGsonConverter(Gson gson, String encoding) {
    super(gson, encoding);
    mGson = gson;
}

@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
    boolean willCloseStream = false; // try to close the stream, if there is no exception thrown using tolerant  JsonReader
    try {
        String mDirty = toString(body);
        if (TextUtils.isEmpty(mDirty)) return null;
        String clean = mDirty.replaceAll("(^\\(|\\)$)", "");
        body = new JsonTypedInput(clean.getBytes(Charset.forName("UTF-8")));
        JsonReader jsonReader = new JsonReader(new InputStreamReader(body.in()));
        jsonReader.setLenient(true);
        Object o = mGson.fromJson(jsonReader, type);
        willCloseStream = true;
        return o;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (willCloseStream) {
            closeStream(body);
        }
    }
}

private String toString(TypedInput body) {
    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    String line;
    try {
        br = new BufferedReader(new InputStreamReader(body.in()));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}

private void closeStream(TypedInput body) {
    try {
        InputStream in = body.in();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

创建CustomErrorHandler.class

public class CustomErrorHandler implements ErrorHandler {
private final Context mContext;

public CustomErrorHandler(Context context) {
    this.mContext = context;
}

@Override
public Throwable handleError(RetrofitError cause) {
    String errorDescription;

    if (cause.isNetworkError()) {
        errorDescription = mContext.getString(R.string.error_network);
    } else {
        if (cause.getResponse() == null) {
            errorDescription = mContext.getString(R.string.error_no_response);
        } else {
            // Error message handling - return a simple error to Retrofit handlers..
            try {
                ErrorResponse errorResponse = (ErrorResponse) cause.getBodyAs(ErrorResponse.class);
                errorDescription = errorResponse.error.data.message;
            } catch (Exception ex) {
                try {
                    errorDescription = mContext.getString(R.string.error_network_http_error, cause.getResponse().getStatus());
                } catch (Exception ex2) {
                    DebugTool.logD("handleError: " + ex2.getLocalizedMessage());
                    errorDescription = mContext.getString(R.string.error_unknown);
                }
            }
        }
    }

    return new Exception(errorDescription);
}

public class ErrorResponse{
    Error error;

    public class Error {
        Data data;

        public class Data {
            String message;
        }
    }
}
}

创建JsonTypedInput.class

public class JsonTypedInput implements TypedInput {

private final byte[] mStringBytes;

JsonTypedInput(byte[] stringBytes) {
    this.mStringBytes = stringBytes;
}


@Override
public String mimeType() {
    return "application/json; charset=UTF-8";
}

@Override
public long length() {
    return mStringBytes.length;
}

@Override
public InputStream in() throws IOException {
    return new ByteArrayInputStream(mStringBytes);
}

}

添加到gradle.build

compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.google.code.gson:gson:1.7.2'
MainActivity.class中的

public class MainActivity extends ListActivity {

private List<Result> mListResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListResult = new ArrayList<Result>();
    new GetDataAsync(this).execute();
}

    private class GetDataAsync extends AsyncTask<Void, Integer, Void> {
    private final WeakReference<SplashActivity> mWeakReference;
    private List<Result> mApiResult;

    public GetDataAsync(MainActivity mainActivity) {
        mWeakReference = new WeakReference< MainActivity >(mainActivity);

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected Void doInBackground(Void... params) {
        MainActivity activity = mWeakReference.get();

        if (activity != null) {
         mApiResult = activity.mApiService.getAllDataFromServer();
        }

 }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
  adapter adapt = new adapter(getApplicationContext(), R.layout.item_file, mListResult);
            setListAdapter(adapt);
 }
 }

类接口

public interface api {
@GET("/GetUserGroupList")
List<Result> getAllData();
}

您应该创建customAdapter

public class CustomAdapter extends ArrayAdapter<Result> {
private Context mContext;
private List<Result> mListResult;
private int saveStateItem = 1;

public CustomAdapter(Context context, int resource, List<Result> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.mListResult = objects;
}


@Override
public int getCount() {
    return mListResult.size();
}

@Override
public Result getItem(int position) {
    return mListResult.get(position);
}

@Override
public View getView(final int position, View view, final ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item_menu, null);
        holder = new ViewHolder();
        holder.mType = (TextView) view.findViewById(R.id.textview_type);
        holder.mName = (TextView) view.findViewById(R.id.textview_name);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    Result cate = mListResult.get(position);
    holder.mType.setText(cate.getFieldTypeID());
    holder.mName.setText(cate.getName());


    return view;
}

public class ViewHolder {
    public TextView mType;
    public TextView mName;
}
}
MainActivity中的

public class MainActivity extends Activity
activity_main.xml中的

。您必须更改列表视图的ID

<ListView
android:id="@+id/listview"
>

以及方法onPostExecute

  @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
  CustomAdapter adapt = new CustomAdapter(getApplicationContext(), R.layout.item_file, mListResult);
            yourListView.setAdapter(adapt);
 }