Android JSON到文本视图?
我让我的应用程序从链接获取JSON数据,但我需要从JSON获取状态Android JSON到文本视图?,android,android-studio,Android,Android Studio,我让我的应用程序从链接获取JSON数据,但我需要从JSON获取状态 状态>代码和消息 以下是我的工作: 截图: 我需要将其输出> 代码:成功(或失败,不管json怎么说) Msg:许可证已更新。。(或者不管JSON怎么说) 任何帮助都会很好,或者给我指点方向,谢谢 我的一些代码: public class doit extends AsyncTask<Void,Void,Void>{ String words; public doit() throws JSO
- 状态>代码和消息
public class doit extends AsyncTask<Void,Void,Void>{
String words;
public doit() throws JSONException {
}
@Override
protected Void doInBackground(Void... voids) {
try {
WebView wv = (WebView) findViewById(R.id.webview);
EditText messages = (EditText) findViewById(R.id.messages);
Document doc = Jsoup.connect("https://api.deepbot.tv/web/apply/namerefresh.php?s=" + messages.getText().toString()).get();
words=doc.text();
}catch(Exception e){e.printStackTrace();}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
texx.setText(words);
}
public类doit扩展异步任务{
字符串;
public doit()抛出JSONException{
}
@凌驾
受保护的空位背景(空位…空位){
试一试{
WebView wv=(WebView)findviewbyd(R.id.WebView);
EditText消息=(EditText)findViewById(R.id.messages);
Document doc=Jsoup.connect(“https://api.deepbot.tv/web/apply/namerefresh.php?s=“+messages.getText().toString()).get();
words=doc.text();
}catch(异常e){e.printStackTrace();}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
texx.setText(字);
}
尝试以下操作,只需更换您的退货即可:
public class doit extends AsyncTask<Void,Void,Void>{
String words;
public doit() throws JSONException {
}
@Override
protected Void doInBackground(Void... voids) {
try {
WebView wv = (WebView) findViewById(R.id.webview);
EditText messages = (EditText) findViewById(R.id.messages);
Document doc = Jsoup.connect("https://api.deepbot.tv/web/apply/namerefresh.php?s=" + messages.getText().toString()).get();
words=doc.text();
}catch(Exception e){e.printStackTrace();}
return words;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
JSONObject jObj = new JSONObject(words);
String code = jObj.getString("code");
String name = jObj.getString("msg");
texx.setText(code +" "+name);
} catch (JSONException e) {
e.printStackTrace();
}
}
public类doit扩展异步任务{
字符串;
public doit()抛出JSONException{
}
@凌驾
受保护的空位背景(空位…空位){
试一试{
WebView wv=(WebView)findviewbyd(R.id.WebView);
EditText消息=(EditText)findViewById(R.id.messages);
Document doc=Jsoup.connect(“https://api.deepbot.tv/web/apply/namerefresh.php?s=“+messages.getText().toString()).get();
words=doc.text();
}catch(异常e){e.printStackTrace();}
返回单词;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
试一试{
JSONObject jObj=新JSONObject(文字);
字符串代码=jObj.getString(“代码”);
字符串名称=jObj.getString(“msg”);
texx.setText(代码+“”+名称);
}捕获(JSONException e){
e、 printStackTrace();
}
}
签出库
您只需要为响应创建对象,就可以得到如下结果
Response response = gson.fromJson("words", Response.class);
响应包含您的所有需求
或
你可以和我一起去
大宗报价
大宗报价
1.首先创建一个带有响应字符串
words
的JSONObject
JSONObject jsonObject = new JSONObject(words);
2.从您的回复中获取JSONObject状态
,JSONObject单词
:
JSONObject jsonObjectStatus = jsonObject.getJSONObject("status");
3.最后,从status
JSONObject解析code
和msg
字符串,如下所示:
String code = jsonObjectStatus.getString("code");
String msg = jsonObjectStatus.getString("msg");
更新onPostExecute()
如下所示:
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
JSONObject jsonObject = new JSONObject(words);
JSONObject jsonObjectStatus = jsonObject.getJSONObject("status");
String code = jsonObjectStatus.getString("code");
String msg = jsonObjectStatus.getString("msg");
texx.setText("Code: " + code + "\nMessage: " + msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
评论不适用于长时间的讨论;此对话已结束。
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
JSONObject jsonObject = new JSONObject(words);
JSONObject jsonObjectStatus = jsonObject.getJSONObject("status");
String code = jsonObjectStatus.getString("code");
String msg = jsonObjectStatus.getString("msg");
texx.setText("Code: " + code + "\nMessage: " + msg);
} catch (JSONException e) {
e.printStackTrace();
}
}