Android 从响应更新中获取单个JSON值
我试图通过改型从JSON中获取一个值,我遵循了一个教程,但我得到了一个错误,即“从回调派生的类匿名类必须声明……”。 我特别想要实现的是在一个空字符串中回显一个json属性值,比如Android 从响应更新中获取单个JSON值,android,json,retrofit,retrofit2,Android,Json,Retrofit,Retrofit2,我试图通过改型从JSON中获取一个值,我遵循了一个教程,但我得到了一个错误,即“从回调派生的类匿名类必须声明……”。 我特别想要实现的是在一个空字符串中回显一个json属性值,比如string Axe=“”并使用服务器json文件中的特定值填充它。这是我试过的 Json格式 "axe1": {"test1"} API接口 import com.google.gson.JsonObject; import retrofit2.Call; import retrofit2.http.GET;
string Axe=“”代码>并使用服务器json文件中的特定值填充它。这是我试过的
Json格式
"axe1": {"test1"}
API接口
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiInterface {
@GET("test.json")
Call<JsonObject> readJsonFromFileUri();
}
import com.google.gson.JsonObject;
2.电话;;
导入文件2.http.GET;
公共接口{
@GET(“test.json”)
调用readJsonFromFileUri();
}
主要活动
import android.graphics.Typeface;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Response;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends ActionBarActivity {
DataBaseHandler db;
private AlertDialog dialog;
public static final int IntialQteOfDayId = 8;
private ImageView btn_quotes, btn_authors, btn_favorites, btn_categories, btn_qteday, btn_rateus ;
final Context context = this;
SharedPreferences preferences;
private static final int RESULT_SETTINGS = 1;
// URL of object to be parsed
// This string will hold the results
String data = "";
class Myads{
String bnr;
String intt;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://yourdomain.com/s/ ")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<JsonObject> jsonCall = apiInterface.readJsonFromFileUri();
jsonCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
JsonObject json = new JsonObject(body().toString());
Gson gson = new Gson();
Myads ad = gson.fromJson(jsonString, Myads.class);
Log.i(LOG_TAG, String.valueOf(ad.bnr));
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e(LOG_TAG, t.toString());
}
});
Typeface bold = Typeface.createFromAsset(getAssets(),
"fonts/extrabold.otf");
db = new DataBaseHandler(this);
db.openDataBase() ;
TextView cat = (TextView) findViewById(R.id.titlecat);
cat.setTypeface(bold);
TextView alls = (TextView) findViewById(R.id.titlest);
alls.setTypeface(bold);
TextView fav = (TextView) findViewById(R.id.titlefav);
fav.setTypeface(bold);
TextView qday = (TextView) findViewById(R.id.titleqday);
qday.setTypeface(bold);
TextView rate = (TextView) findViewById(R.id.titleqrate);
rate.setTypeface(bold);
btn_quotes = (ImageView) findViewById(R.id.btn_quotes);
//btn_authors= (Button) findViewById(R.id.btn_authors);
btn_categories = (ImageView) findViewById(R.id.btn_categories);
btn_favorites = (ImageView) findViewById(R.id.btn_favorites);
btn_qteday = (ImageView) findViewById(R.id.btn_qteday);
btn_rateus = (ImageView) findViewById(R.id.btn_rateus);
btn_quotes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
QuotesActivity.class);
intent.putExtra("mode", "allQuotes");
startActivity(intent);
}
});
/*btn_authors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent author = new Intent(MainActivity.this,
AuteursActivity.class);
startActivity(author);
}
});*/
btn_favorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent favorites = new Intent(MainActivity.this,
QuotesActivity.class);
favorites.putExtra("mode", "isFavorite");
startActivity(favorites);
}
});
btn_categories.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent category = new Intent(MainActivity.this,
CategoryActivity.class);
startActivity(category);
}
});
btn_qteday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
preferences = PreferenceManager
.getDefaultSharedPreferences(context);
Intent qteDay = new Intent(MainActivity.this,
QuoteActivity.class);
qteDay.putExtra("id",
preferences.getInt("id", IntialQteOfDayId));
qteDay.putExtra("mode", "qteday");
startActivity(qteDay);
}
});
btn_rateus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage(getResources().getString(
R.string.ratethisapp_msg));
builder.setTitle(getResources().getString(
R.string.ratethisapp_title));
builder.setPositiveButton(
getResources().getString(R.string.rate_it),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Intent fire = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())); //dz.amine.thequotesgarden"));
startActivity(fire);
}
});
builder.setNegativeButton(
getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_settings) {
Intent i = new Intent(this, UserSettingActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
}
return super.onOptionsItemSelected(item);
}
}
导入android.graphics.Typeface;
导入android.os.Build;
导入android.support.v7.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.net.Uri;
导入android.preference.PreferenceManager;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.WindowManager;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.android.volley.Response;
导入com.google.gson.gson;
导入com.google.gson.JsonObject;
2.电话;;
2.回拨;
进口改装2.改装;
进口改装2.converter.gson.GsonConverterFactory;
公共类MainActivity扩展了ActionBarActivity{
数据库处理程序数据库;
私人警报对话框;
公共静态最终int initialqteofdayid=8;
私有图像查看btn_引用、btn_作者、btn_收藏夹、btn_类别、btn_qteday、btn_评级;
最终上下文=此;
共享参考偏好;
私有静态最终整数结果_设置=1;
//要分析的对象的URL
//此字符串将保存结果
字符串数据=”;
类Myads{
字符串bnr;
字符串intt;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u DRAWS\u SYSTEM\u BAR\u BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
改装改装=新改装.Builder()
.baseUrl(“https://yourdomain.com/s/ ")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface ApiInterface=reformation.create(ApiInterface.class);
调用jsonCall=apinterface.readJsonFromFileUri();
jsonCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
JsonObject json=新的JsonObject(body().toString());
Gson Gson=新的Gson();
Myads ad=gson.fromJson(jsonString,Myads.class);
Log.i(Log_标记,String.valueOf(ad.bnr));
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(Log_标记,t.toString());
}
});
Typeface bold=Typeface.createFromAsset(getAssets(),
“字体/extrabold.otf”);
db=新的DataBaseHandler(此);
openDataBase();
TextView cat=(TextView)findViewById(R.id.titlecat);
类别设置字体(粗体);
TextView alls=(TextView)findViewById(R.id.titest);
设置字体(粗体);
TextView fav=(TextView)findViewById(R.id.titlefav);
字体(粗体);
TextView qday=(TextView)findViewById(R.id.titleqday);
qday.setTypeface(粗体);
TextView比率=(TextView)findViewById(R.id.titleqrate);
速率。设置字体(粗体);
btn_quotes=(ImageView)findViewById(R.id.btn_quotes);
//btn_authors=(按钮)findViewById(R.id.btn_authors);
btn_categories=(ImageView)findViewById(R.id.btn_categories);
btn_收藏夹=(ImageView)findViewById(R.id.btn_收藏夹);
btn_qteday=(ImageView)findViewById(R.id.btn_qteday);
btn_rateus=(ImageView)findViewById(R.id.btn_rateus);
btn_quotes.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(MainActivity.this,
QuotesActivity.class);
intent.putExtra(“模式”、“所有报价”);
星触觉(意向);
}
});
/*btn_authors.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意图作者=新意图(MainActivity.this,
自主性(阶级);
星触觉(作者);
}
});*/
btn_favorites.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向收藏夹=新意向(MainActivity.this,
QuotesActivity.class);
favorites.putExtra(“mode”,“isFavorite”);
星触觉(最爱);
}
});
btn_categories.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向类别=新意向(MainActivity.this,
类别活动(类别);
星触觉(类别);
}
});
btn_qteday.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
首选项=首选项管理器
.GetDefaultSharedReferences(上下文);
意向qteDay=新意向(MainActivity.this,
QuoteActivity.class);
qteDay.putExtra(“id”,
preferences.getInt(“id”,initialqteofdayid));
import com.android.volley.Response;
import retrofit2.Response;
JSONObject json = new JSONObject(response.body().toString());
Log.i(LOG_TAG, json.getString("axe1"));