我正在努力让AdWhirl库工作。日志显示各种异常,但即使这样,应用程序实际上也可以。它只是没有显示任何广告。这是我现在正在使用的代码:
private void adStuff() {
LinearLayout mAdLayout = (LinearLayout) findViewById(R.id.AdLayout);
AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
RelativeLayout.LayoutParams adWhirlLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int mAdWidth = 320;
int mAdHeight = 75;
AdWhirlTargeting.setAge(23);
AdWhirlTargeting.setGender(AdWhirlTargeting.Gender.MALE);
AdWhirlTargeting.setKeywords("android");
AdWhirlTargeting.setPostalCode("47007");
AdWhirlTargeting.setTestMode(false);
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
mAdHeight = mDisplayMetrics.heightPixels;
float mDensity = mDisplayMetrics.density;
mAdWidth = mDisplayMetrics.widthPixels;
mAdHeight = (int) (mAdHeight * mDensity);
AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this,
getResources().getString(R.string.adwhirlid));
adWhirlLayout.setAdWhirlInterface(this);
adWhirlLayout.setMaxWidth(mAdWidth);
adWhirlLayout.setMaxHeight(mAdHeight);
adWhirlLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mAdLayout.setGravity(Gravity.CENTER_HORIZONTAL);
mAdLayout.addView(adWhirlLayout, adWhirlLayoutParams);
mAdLayout.invalidate();
}
我在onCreate()的末尾调用此方法。
这是我的主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buscar"
android:background="@drawable/auvasa_button"/>
<Button
android:id="@+id/buttonList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/listaparadas"
android:background="@drawable/auvasa_button"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/AdLayout">
这是我得到的一堆例外的日志:
03-26 12:37:30.929: E/AdWhirl SDK(5705): Caught IOException in fetchConfig()
03-26 12:37:30.929: E/AdWhirl SDK(5705): org.apache.http.conn.HttpHostConnectException: Connection to http://mob.adwhirl.com refused
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at com.adwhirl.AdWhirlManager.fetchConfig(AdWhirlManager.java:232)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at com.adwhirl.AdWhirlLayout$InitRunnable.run(AdWhirlLayout.java:376)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:150)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:264)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.lang.Thread.run(Thread.java:856)
03-26 12:37:30.929: E/AdWhirl SDK(5705): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at libcore.io.IoBridge.connect(IoBridge.java:114)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.net.Socket.connect(Socket.java:842)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
03-26 12:37:30.929: E/AdWhirl SDK(5705): ... 16 more
03-26 12:37:30.929: E/AdWhirl SDK(5705): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at libcore.io.Posix.connect(Native Method)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-26 12:37:30.929: E/AdWhirl SDK(5705): ... 21 more
03-26 12:37:30.929: D/AdWhirl SDK(5705): Received jsonString: null
03-26 12:37:30.929: E/AdWhirl SDK(5705): Unable to parse response from JSON. This may or may not be fatal.
03-26 12:37:30.929: E/AdWhirl SDK(5705): java.lang.NullPointerException
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.json.JSONObject.<init>(JSONObject.java:154)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at org.json.JSONObject.<init>(JSONObject.java:171)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at com.adwhirl.AdWhirlManager.parseConfigurationString(AdWhirlManager.java:290)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at com.adwhirl.AdWhirlManager.fetchConfig(AdWhirlManager.java:257)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at com.adwhirl.AdWhirlLayout$InitRunnable.run(AdWhirlLayout.java:376)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:150)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:264)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-26 12:37:30.929: E/AdWhirl SDK(5705): at java.lang.Thread.run(Thread.java:856)
有谁能告诉我我做错了什么?我修改了代码几十次,我无法让它工作。我知道服务器返回一个null json,我真的不知道为什么。