二阶段团队冲刺(3)
今天继续垃圾分类的新闻和语音功能
语音功能
package com.example.listviewtest; import com.iflytek.cloud.RecognizerResult; public interface XunFeiCallbackListener { void onFinish(RecognizerResult results); }
package com.example.listviewtest; import android.content.Context; import android.util.Log; import android.widget.Toast; import com.iflytek.cloud.RecognizerResult; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechUtility; import com.iflytek.cloud.ui.RecognizerDialog; import com.iflytek.cloud.ui.RecognizerDialogListener; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; //jniLibs public class XunFeiUtil { public static String appid = "7ffd5261"; public static void initXunFei(Context context){ SpeechUtility.createUtility(context, SpeechConstant.APPID +"=7ffd5261"); } public static void startVoice(Context context, final XunFeiCallbackListener callbackListener) { RecognizerDialog dialog = new RecognizerDialog(context,null); Log.d("shabi",dialog.toString()); dialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); dialog.setParameter(SpeechConstant.ACCENT, "mandarin"); dialog.setParameter(SpeechConstant.ASR_PTT, "0"); dialog.setListener(new RecognizerDialogListener() { @Override public void onResult(RecognizerResult recognizerResult, boolean b) { callbackListener.onFinish(recognizerResult); } @Override public void onError(SpeechError speechError) { } }); dialog.show(); //Toast.makeText(this, "璇峰紑濮嬭璇?, Toast.LENGTH_SHORT).show(); } public static String parseIatResult(String json) { StringBuffer ret = new StringBuffer(); try { JSONTokener tokener = new JSONTokener(json); JSONObject joResult = new JSONObject(tokener); JSONArray words = joResult.getJSONArray("ws"); for (int i = 0; i < words.length(); i++) { // 杞啓缁撴灉璇嶏紝榛樿浣跨敤绗竴涓粨鏋? JSONArray items = words.getJSONObject(i).getJSONArray("cw"); JSONObject obj = items.getJSONObject(0); ret.append(obj.getString("w")); } } catch (Exception e) { e.printStackTrace(); } return ret.toString(); } }
语音功能出现bug,准备修改
新闻功能
主页
package com.llw.goodtrash.contract; import android.annotation.SuppressLint; import com.llw.goodtrash.api.ApiService; import com.llw.goodtrash.model.TrashNewsResponse; import com.llw.goodtrash.model.TrashResponse; import com.llw.mvplibrary.base.BasePresenter; import com.llw.mvplibrary.base.BaseView; import com.llw.mvplibrary.network.NetworkApi; import com.llw.mvplibrary.network.observer.BaseObserver; /** * 主页面访问网络 */ public class MainContract { public static class MainPresenter extends BasePresenter<MainView> { /** * 垃圾分类新闻 * * @param num 数量 */ @SuppressLint("CheckResult") public void getTrashNews(Integer num) { ApiService service = NetworkApi.createService(ApiService.class,0); service.getTrashNews(num).compose(NetworkApi.applySchedulers(new BaseObserver<TrashNewsResponse>() { @Override public void onSuccess(TrashNewsResponse trashNewsResponse) { if (getView() != null) { getView().getTrashNewsResponse(trashNewsResponse); } } @Override public void onFailure(Throwable e) { if (getView() != null) { getView().getTrashNewsFailed(e); } } })); } } public interface MainView extends BaseView { /** * 获取垃圾分类新闻返回 * * @param response */ void getTrashNewsResponse(TrashNewsResponse response); /** * 搜索物品异常返回 * * @param throwable */ void getTrashNewsFailed(Throwable throwable); } }
@Override protected MainContract.MainPresenter createPresenter() { return new MainContract.MainPresenter(); } /** * 获取垃圾分类新闻成功返回 * * @param response */ @Override public void getTrashNewsResponse(TrashNewsResponse response) { if (response.getCode() == Constant.SUCCESS_CODE) { List<TrashNewsResponse.NewslistBean> list = response.getNewslist(); if (list.size() > 0) { //数据显示 } else { showMsg("垃圾分类新闻为空"); } } else { showMsg(response.getMsg()); } } /** * 获取垃圾分类新闻失败返回 * * @param throwable 异常 */ @Override public void getTrashNewsFailed(Throwable throwable) { Log.d(TAG, "获取垃圾分类新闻失败:" + throwable.toString()); }
然后在initData中调用请求。
@Override
public void initData(Bundle savedInstanceState) {
mPresenter.getTrashNews(10);
}
那么现在页面上的数据请求和返回就都有了,对于这个数据可以设置两个显示的效果,第一个是顶部的轮播,第二个是页面的列表显示。先来完成轮播的显示。
新闻功能部分实现,语音功能出现bug