我试图将值从Android手机发布到我的网络服务器。我在java.io.exception中遇到403错误。
这是我的logcat消息
[CDS]connect[local.test.com/192.99.166.230:80] tm:90
04-30 12:57:07.388 10416-10434/gcm.androidexample.com.ravi D/Posix﹕ [Posix_connect Debug]Process gcm.androidexample.com.ravi :80
04-30 12:57:07.413 10416-10416/gcm.androidexample.com.ravi V/InputMethodManager﹕ onWindowFocus: null softInputMode=288 first=true flags=#1810100
04-30 12:57:07.413 10416-10416/gcm.androidexample.com.ravi D/InputMethodManager﹕ deactivate the inputconnection in ControlledInputConnectionWrapper.
04-30 12:57:07.413 10416-10416/gcm.androidexample.com.ravi V/InputMethodManager﹕ START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{41cd4600 V.E..... R.....ID 0,0-480,800} ic=null tba=android.view.inputmethod.EditorInfo@41cfca48 controlFlags=#104
04-30 12:57:07.416 10416-10416/gcm.androidexample.com.ravi V/InputMethodManager﹕ Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41cfd2d8 com.android.inputmethod.latin/.LatinIME #186}
04-30 12:57:07.513 10416-10416/gcm.androidexample.com.ravi D/OpenGLRenderer﹕ Flushing caches (mode 0)
04-30 12:57:07.696 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][/192.168.1.70:36222] connected
04-30 12:57:07.696 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]rx timeout:0
04-30 12:57:07.820 10416-10416/gcm.androidexample.com.ravi D/OpenGLRenderer﹕ Flushing caches (mode 0)
04-30 12:57:08.012 10416-10434/gcm.androidexample.com.ravi D/AndroidRuntime﹕ zoneinfo:Asia/Calcutta
04-30 12:57:08.014 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]close[36222]
04-30 12:57:08.015 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ close [socket][/0.0.0.0:36222]
04-30 12:57:08.015 10416-10434/gcm.androidexample.com.ravi E/GCM Android Example﹕ Failed to register on attempt 1:java.io.IOException: Post failed with error code 403
04-30 12:57:08.015 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Sleeping for 2401 ms before retry
04-30 12:57:10.416 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Attempt #2 to register
04-30 12:57:10.418 10416-10434/gcm.androidexample.com.ravi V/GCM Android Example﹕ Posting 'email=fvgghhgv@hhngfg.hjh®Id=APA91bFKaVLGMwjZ-4DywjSCncNlokYu96l_4MyIFCt1cqCYEnDlCr-WMns4IuuEVAyCOl8XGKgOKptfbUxHgiDS_qy65C48uRPsl3NCZRuOhtswsEuJZLSq2x5w0i-znjhdqYFhDwSUJHrr8ye-2vgMP6duXCW9pw&name=gfgh' to
04-30 12:57:10.418 10416-10434/gcm.androidexample.com.ravi E/URL﹕ >
04-30 12:57:10.425 10416-10434/gcm.androidexample.com.ravi D/libc-netbsd﹕ getaddrinfo: local.test.com get result from proxy >>
04-30 12:57:10.426 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ propertyValue:true
04-30 12:57:10.426 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][1] connection local.test.com/192.99.166.230:80;LocalPort=34236(0)
04-30 12:57:10.427 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ /192.99.166.230:80] tm:90
04-30 12:57:10.427 10416-10434/gcm.androidexample.com.ravi D/Posix﹕ [Posix_connect Debug]Process gcm.androidexample.com.ravi :80
04-30 12:57:10.460 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x52c45438) fps:0.33, dur:3015.55, max:3015.55, min:3015.55
04-30 12:57:10.733 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][/192.168.1.70:34236] connected
04-30 12:57:10.733 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]rx timeout:0
04-30 12:57:11.045 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]close[34236]
04-30 12:57:11.046 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ close [socket][/0.0.0.0:34236]
04-30 12:57:11.046 10416-10434/gcm.androidexample.com.ravi E/GCM Android Example﹕ Failed to register on attempt 2:java.io.IOException: Post failed with error code 403
04-30 12:57:11.047 10416-10434/gcm.androidexample.com.ravi D/GCM Android xample﹕ Sleeping for 4802 ms before retry
04-30 12:57:15.849 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Attempt #3 to register
04-30 12:57:15.873 10416-10434/gcm.androidexample.com.ravi V/GCM Android Example﹕ Posting 'email=fvgghhgv@hhngfg.hjh®Id=APA91bFKaVLGMwjZ-4DywjSCncNlokYu96l_4MyIFCt1cqCYEnDlCr-WMns4IuuEVAyCOl8XGKgOKptfbUxHgiDS_qy65C48uRPsl3NCZRuOhtswsEuJZLSq2x5w0i-znjhdqYFhDwSUJHrr8ye-2vgMP6duXCW9pw&name=gfgh' to
04-30 12:57:15.885 10416-10434/gcm.androidexample.com.ravi E/URL﹕ >
04-30 12:57:15.889 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x52c45438) fps:0.18, dur:5428.43, max:5428.43, min:5428.43
04-30 12:57:15.889 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] this:0x52c45438, api:1, last queue time elapsed:5428.43
04-30 12:57:15.891 10416-10434/gcm.androidexample.com.ravi D/libc-netbsd﹕ getaddrinfo: local.test.com get result from proxy >>
04-30 12:57:15.891 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ propertyValue:true
04-30 12:57:15.892 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][2] connection local.test.com/192.99.166.230:80;LocalPort=44926(0)
04-30 12:57:15.892 10416-10434/gcm.androidexample.com.ravi I/System.out﹕
04-30 12:57:15.893 10416-10434/gcm.androidexample.com.ravi D/Posix﹕ [Posix_connect Debug]Process gcm.androidexample.com.ravi :80
04-30 12:57:16.202 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][/192.168.1.70:44926] connected
04-30 12:57:16.202 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]rx timeout:0
04-30 12:57:16.503 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]close[44926]
04-30 12:57:16.504 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ close [socket][/0.0.0.0:44926]
04-30 12:57:16.504 10416-10434/gcm.androidexample.com.ravi E/GCM Android Example﹕ Failed to register on attempt 3:java.io.IOException: Post failed with error code 403
04-30 12:57:16.505 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Sleeping for 9604 ms before retry
04-30 12:57:26.109 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Attempt #4 to register
04-30 12:57:26.140 10416-10434/gcm.androidexample.com.ravi V/GCM Android Example﹕ Posting 'email=fvgghhgv@hhngfg.hjh®Id=APA91bFKaVLGMwjZ-4DywjSCncNlokYu96l_4MyIFCt1cqCYEnDlCr-WMns4IuuEVAyCOl8XGKgOKptfbUxHgiDS_qy65C48uRPsl3NCZRuOhtswsEuJZLSq2x5w0i-znjhdqYFhDwSUJHrr8ye-2vgMP6duXCW9pw&name=gfgh' to
04-30 12:57:26.145 10416-10434/gcm.androidexample.com.ravi E/URL﹕ >
I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x52c45438) fps:0.10, dur:10271.53, max:10271.53, min:10271.53
04-30 12:57:26.160 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] this:0x52c45438, api:1, last queue time elapsed:10271.53
04-30 12:57:26.162 10416-10434/gcm.androidexample.com.ravi D/libc-netbsd﹕ getaddrinfo: local.test.com get result from proxy >>
04-30 12:57:26.165 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ propertyValue:true
04-30 12:57:26.166 10416-10434/gcm.androidexample.com.ravi I/System.out﹕
04-30 12:57:26.166 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]connect[local.test.com/192.99.166.230:80] tm:90
04-30 12:57:26.174 10416-10434/gcm.androidexample.com.ravi D/Posix﹕ [Posix_connect Debug]Process gcm.androidexample.com.ravi :80
04-30 12:57:26.492 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][/192.168.1.70:45892] connected
04-30 12:57:26.492 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]rx timeout:0
04-30 12:57:26.806 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]close[45892]
04-30 12:57:26.806 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ close [socket][/0.0.0.0:45892]
04-30 12:57:26.807 10416-10434/gcm.androidexample.com.ravi E/GCM Android Example﹕ Failed to register on attempt 4:java.io.IOException: Post failed with error code 403
04-30 12:57:26.807 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Sleeping for 19208 ms before retry
04-30 12:57:46.015 10416-10434/gcm.androidexample.com.ravi D/GCM Android Example﹕ Attempt #5 to register
04-30 12:57:46.044 10416-10434/gcm.androidexample.com.ravi V/GCM Android Example﹕ Posting 'email=fvgghhgv@hhngfg.hjh®Id=APA91bFKaVLGMwjZ-4DywjSCncNlokYu96l_4MyIFCt1cqCYEnDlCr-WMns4IuuEVAyCOl8XGKgOKptfbUxHgiDS_qy65C48uRPsl3NCZRuOhtswsEuJZLSq2x5w0i-znjhdqYFhDwSUJHrr8ye-2vgMP6duXCW9pw&name=gfgh' to
04-30 12:57:46.051 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x52c45438) fps:0.05, dur:19890.52, max:19890.52, min:19890.52
04-30 12:57:46.051 10416-10436/gcm.androidexample.com.ravi I/SurfaceTextureClient﹕ [STC::queueBuffer] this:0x52c45438, api:1, last queue time elapsed:19890.52
04-30 12:57:46.060 10416-10434/gcm.androidexample.com.ravi E/URL﹕ > http://local.test.com/gcmfile/register
04-30 12:57:46.065 10416-10434/gcm.androidexample.com.ravi D/libc-netbsd﹕
04-30 12:57:46.066 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ propertyValue:true
04-30 12:57:46.066 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][4] connection local.test.com/192.99.166.230:80;LocalPort=38220(0)
04-30 12:57:46.066 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]connect[local.test.com/192.99.166.230:80] tm:90
04-30 12:57:46.067 10416-10434/gcm.androidexample.com.ravi D/Posix﹕ [Posix_connect Debug]Process gcm.androidexample.com.ravi :80
04-30 12:57:46.390 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [socket][/192.168.1.70:38220] connected
04-30 12:57:46.393 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]rx timeout:0
04-30 12:57:46.701 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ [CDS]close[38220]
04-30 12:57:46.702 10416-10434/gcm.androidexample.com.ravi I/System.out﹕ close [socket][/0.0.0.0:38220]
04-30 12:57:46.702 10416-10434/gcm.androidexample.com.ravi E/GCM Android Example﹕ Failed to register on attempt 5:java.io.IOException: Post failed with error code 403
这是我在寄存器上的代码
package gcm.androidexample.com.ravi;
/**
* Created by Thidiff on 4/24/2015.
*/
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.PowerManager;
import android.util.Log;
import com.google.android.gcm.GCMRegistrar;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import gcm.androidexample.com.gcm.R;
public class Controller extends Application{
private final int MAX_ATTEMPTS = 5;
private final int BACKOFF_MILLI_SECONDS = 2000;
private final Random random = new Random();
// Register this account with the server.
void register(final Context context, String name, String email, final String regId) {
Log.i(Config.TAG, "registering device (regId = " + regId + ")");
String serverUrl = Config.YOUR_SERVER_URL;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("name", name);
params.put("email", email);
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
String res = "";
// Once GCM returns a registration id, we need to register on our server
// As the server might be down, we will retry it a couple
// times.
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
Log.d(Config.TAG, "Attempt #" + i + " to register");
try {
//Send Broadcast to Show message on screen
displayMessageOnScreen(context, context.getString(
R.string.server_registering, i, MAX_ATTEMPTS));
// Post registration values to web server
post(serverUrl, params);
GCMRegistrar.setRegisteredOnServer(context, true);
//Send Broadcast to Show message on screen
String message = context.getString(R.string.server_registered);
displayMessageOnScreen(context, message);
return;
} catch (IOException e) {
// Here we are simplifying and retrying on any error; in a real
// application, it should retry only on unrecoverable errors
// (like HTTP error code 503).
Log.e(Config.TAG, "Failed to register on attempt " + i + ":" + e);
if (i == MAX_ATTEMPTS) {
break;
}
try {
Log.d(Config.TAG, "Sleeping for " + backoff + " ms before retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
Log.d(Config.TAG, "Thread interrupted: abort remaining retries!");
Thread.currentThread().interrupt();
return;
}
// increase backoff exponentially
backoff *= 2;
}
}
String message = context.getString(R.string.server_register_error,
MAX_ATTEMPTS);
//Send Broadcast to Show message on screen
displayMessageOnScreen(context, message);
}
// Unregister this account/device pair within the server.
void unregister(final Context context, final String regId) {
Log.i(Config.TAG, "unregistering device (regId = " + regId + ")");
String serverUrl = Config.YOUR_SERVER_URL + "/unregister";
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
try {
post(serverUrl, params);
GCMRegistrar.setRegisteredOnServer(context, false);
String message = context.getString(R.string.server_unregistered);
displayMessageOnScreen(context, message);
} catch (IOException e) {
// At this point the device is unregistered from GCM, but still
// registered in the our server.
// We could try to unregister again, but it is not necessary:
// if the server tries to send a message to the device, it will get
// a "NotRegistered" error message and should unregister the device.
String message = context.getString(R.string.server_unregister_error,
e.getMessage());
displayMessageOnScreen(context, message);
}
}
// Issue a POST request to the server.
private static void post(String endpoint, Map<String, String> params)
throws IOException {
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
Log.v(Config.TAG, "Posting '" + body + "' to " + url);
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
Log.e("URL", "> " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
// If response is not success
if (status != 200) {
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
// Checking for all possible internet providers
public boolean isConnectingToInternet(){
ConnectivityManager connectivity =
(ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
// Notifies UI to display a message.
void displayMessageOnScreen(Context context, String message) {
Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION);
intent.putExtra(Config.EXTRA_MESSAGE, message);
// Send Broadcast to Broadcast receiver with message
context.sendBroadcast(intent);
}
//Function to display simple Alert Dialog
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Set Dialog Title
alertDialog.setTitle(title);
// Set Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Set alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Set OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Show Alert Message
alertDialog.show();
}
private PowerManager.WakeLock wakeLock;
public void acquireWakeLock(Context context) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "WakeLock");
wakeLock.acquire();
}
public void releaseWakeLock() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
答案 0 :(得分:2)
HTML中的错误403是禁止的,这意味着您的服务器未设置为接受xmlhttp或论坛帖子,或者您使用的服务不正确。