二阶段团队冲刺(2)
1阶段
首先开展新闻功能和语音功能
首先新闻功能部分代码
package com.llw.goodtrash.api; import com.llw.goodtrash.model.TrashNewsResponse; import io.reactivex.Observable; import retrofit2.http.GET; import retrofit2.http.Query; import static com.llw.goodtrash.utils.Constant.KEY; /** * API接口 */ public interface ApiService { /** * 垃圾分类新闻 * * @param num 数量 * @return TrashNewsResponse 结果实体 */ @GET("/lajifenleinews/index?key=" + KEY) Observable<TrashNewsResponse> getTrashNews(@Query("num") Integer num); }
package com.llw.goodtrash.model; import java.util.List; /** * 垃圾分类新闻返回实体 */ public class TrashNewsResponse { /** * code : 200 * msg : success */ private int code; private String msg; private List<NewslistBean> newslist; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<NewslistBean> getNewslist() { return newslist; } public void setNewslist(List<NewslistBean> newslist) { this.newslist = newslist; } public static class NewslistBean { private String id; private String ctime; private String title; private String description; private String source; private String picUrl; private String url; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } }
之后语音部分功能
package com.example.listviewtest; import static com.example.listviewtest.XunFeiUtil.initXunFei; import static com.example.listviewtest.XunFeiUtil.parseIatResult; import static com.example.listviewtest.XunFeiUtil.startVoice; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import com.iflytek.cloud.RecognizerResult; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechRecognizer; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_click; private EditText mResultText; private SpeechRecognizer mIat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initXunFei(this); btn_click = (Button) findViewById(R.id.btn_click); mResultText = ((EditText) findViewById(R.id.result)); btn_click.setOnClickListener(this); } @Override public void onClick(View v) { startVoice(this, new XunFeiCallbackListener() { @Override public void onFinish(RecognizerResult results) { String text = parseIatResult(results.getResultString()); mResultText.append(text); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/btn_click" android:text="鐐瑰嚮鎵撳紑璁璇煶璇嗗埆" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/result" android:layout_below="@id/btn_click" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姝ゅ鏄剧ず浣犳墍褰曚笅鐨勫唴瀹? /> </LinearLayout>
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(); } }
语音功能与新闻功能完成一部分,明天继续