Android中的自定义参数
Posted
技术标签:
【中文标题】Android中的自定义参数【英文标题】:Custom parameters in Android 【发布时间】:2016-06-01 22:20:16 【问题描述】:我正在制作优惠墙 SDK。首先,我正在初始化并获得响应。然后我做了两种显示offerwall的方法。 我的查询是如果开发人员想要任何东西,他可以添加一些自定义参数并从用户那里获取。如何在我的 Url 中添加自定义参数?
我的 initializeSDK 类是:
public class InitializeSDK
/*String json = "";
URL url;
HttpURLConnection connection = null;*/
private static String PREF_NAME = "gallectica_pref_adstuck";
private static SharedPreferences prefs;
public static void init(final Context ctx, final String developer_public_key, final String offerwall_public_key)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheSize(50 * 1024 * 1024) // 50 Mb
.tasksProcessingOrder(QueueProcessingType.LIFO)
//.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
new AsyncTask<Void, Void, Boolean>()
protected void onPreExecute()
super.onPreExecute();
prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
prefs.edit().putString("android_id", Settings.Secure.getString(ctx.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)).commit();
protected Boolean doInBackground(Void... arg0)
//TODO: add code to read http request and store the json data in json variable
String json = "";
HttpURLConnection connection = null;
InputStream is = null;
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("aff_id", prefs.getString("android_id", "")));
params.add(new BasicNameValuePair("offerwall_public_key", offerwall_public_key));
params.add(new BasicNameValuePair("developer_public_key", developer_public_key));
params.add(new BasicNameValuePair("locale", Locale.getDefault().getLanguage()));
try
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://a.nextput.com/api/init/" + developer_public_key + "/a/u");//YOUR URL ?aff_id
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
json = EntityUtils.toString(httpResponse.getEntity());
JSONObject jObj = new JSONObject(json);
boolean isSuccess = jObj.getBoolean("success");
System.out.println("success : " + isSuccess);
/* JSONObject jsonObject = new JSONObject(json);
boolean state = jsonObject.getBoolean("success");*/
return isSuccess;
catch (Exception e)
Log.e("JSON Parser", "Error parsing data " + e.toString());
return false;
protected void onPostExecute(Boolean result)
super.onPostExecute(result);
prefs.edit().putBoolean("isSuccess", result).commit();
if (result)
prefs.edit()
.putString("developer_public_key", developer_public_key)
.putString("offerwall_public_key", offerwall_public_key)
.commit();
.execute();
//Incent Offerwall
public static void showIncentOfferwall(final Context ctx) throws ExceptionInInitializerError
SharedPreferences prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
final String offerwall_public_key = prefs.getString("offerwall_public_key", "4/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G");
final String aff_id = prefs.getString("android_id", "");
if (prefs.getBoolean("isSuccess", false))
Intent intent = new Intent(ctx, WebviewActivity_Incent.class);
intent.putExtra("WEBVIEW_URL", "http://a.nextput.com/api/offerwall/" + offerwall_public_key + "/a/o?aff_id=" + aff_id);
ctx.startActivity(intent);
else
//Throw Exception
throw new ExceptionInInitializerError("Please initialize first to show Incent Offerwalls.");
//NonIncent Offerwall
public static void showNonIncentOfferwall(final Context ctx) throws ExceptionInInitializerError
SharedPreferences prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
final String developer_public_key = prefs.getString("developer_public_key", "23/89533a6f4248873b08ce52ce680f29e7");
final String aff_id = prefs.getString("android_id", "");
if (prefs.getBoolean("isSuccess", false))
Intent intent = new Intent(ctx, WebviewActivity_NonIncent.class);
intent.putExtra("WEBVIEW_URL", "http://a.nextput.com/api/offerwall/" + developer_public_key + "/a/u?aff_id=" + aff_id);
ctx.startActivity(intent);
else
//Throw Exception
throw new ExceptionInInitializerError("Please initialize first to show NonIncent Offerwalls.");
我必须在以下两个方法 showIncentOfferwall 和 showNonIncentOfferwall 的 url 中添加自定义参数。制作任何字符串参数。请帮忙!!
【问题讨论】:
【参考方案1】:我知道你必须做什么。例如,您的 Ball.java 有 3 个字段。你想在 params 中放置一个 Ball 的实例。我不知道一个标准的方法。但是您可以为每个对象编写一个普通的方法链接 toString 。此方法返回一个字符串数组。所以你可以在每个自定义对象中调用这个方法。这样做,您还可以从另一个自定义对象内的自定义对象内的自定义对象中检索参数。
Ora 可能使用 GSON 序列化反序列化对象
【讨论】:
以上是关于Android中的自定义参数的主要内容,如果未能解决你的问题,请参考以下文章